Skip to content

Commit

Permalink
hof/chat: mvp for chatgpt & llm features (#201)
Browse files Browse the repository at this point in the history
🎉
  • Loading branch information
verdverm authored May 8, 2023
1 parent c83488b commit d11b312
Show file tree
Hide file tree
Showing 33 changed files with 1,114 additions and 17 deletions.
95 changes: 95 additions & 0 deletions .hof/shadow/cli/cmd/hof/cmd/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/hofstadter-io/hof/cmd/hof/flags"

"github.com/hofstadter-io/hof/cmd/hof/ga"
)

var chatLong = `Use chat to work with hof features or from modules you import.
Module authors can provide custom prompts for their schemas.
Currently, only ChatGPT is supported. You can use any of the
gpt-3.5 or gpt-4 models. The flag should match OpenAI API options.
Set OPENAI_API_KEY`

func init() {

ChatCmd.Flags().StringVarP(&(flags.ChatFlags.Model), "model", "M", "gpt-3.5-turbo", "LLM model to use [gpt-3.5-turbo,gpt-4]")
ChatCmd.Flags().StringVarP(&(flags.ChatFlags.Prompt), "prompt", "P", "", "path to the system prompt, the first message in the chat")
ChatCmd.Flags().StringVarP(&(flags.ChatFlags.Outfile), "outfile", "O", "", "path to write the output to")
}

func ChatRun(args []string) (err error) {

// you can safely comment this print out
fmt.Println("not implemented")

return err
}

var ChatCmd = &cobra.Command{

Use: "chat [args]",

Short: "Co-design with AI (alpha)",

Long: chatLong,

Run: func(cmd *cobra.Command, args []string) {

ga.SendCommandPath(cmd.CommandPath())

var err error

// Argument Parsing

err = ChatRun(args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
extra := func(cmd *cobra.Command) bool {

return false
}

ohelp := ChatCmd.HelpFunc()
ousage := ChatCmd.UsageFunc()

help := func(cmd *cobra.Command, args []string) {

ga.SendCommandPath(cmd.CommandPath() + " help")

if extra(cmd) {
return
}
ohelp(cmd, args)
}
usage := func(cmd *cobra.Command) error {
if extra(cmd) {
return nil
}
return ousage(cmd)
}

thelp := func(cmd *cobra.Command, args []string) {
help(cmd, args)
}
tusage := func(cmd *cobra.Command) error {
return usage(cmd)
}
ChatCmd.SetHelpFunc(thelp)
ChatCmd.SetUsageFunc(tusage)

}
1 change: 1 addition & 0 deletions .hof/shadow/cli/cmd/hof/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func RootInit() {
RootCmd.AddCommand(FlowCmd)
RootCmd.AddCommand(FmtCmd)
RootCmd.AddCommand(ModCmd)
RootCmd.AddCommand(ChatCmd)
RootCmd.AddCommand(RunCmd)
RootCmd.AddCommand(FeedbackCmd)

Expand Down
9 changes: 9 additions & 0 deletions .hof/shadow/cli/cmd/hof/flags/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package flags

type ChatFlagpole struct {
Model string
Prompt string
Outfile string
}

var ChatFlags ChatFlagpole
99 changes: 99 additions & 0 deletions cmd/hof/cmd/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/hofstadter-io/hof/cmd/hof/flags"

"github.com/hofstadter-io/hof/cmd/hof/ga"

"github.com/hofstadter-io/hof/lib/chat/cmd"
)

var chatLong = `Use chat to work with hof features or from modules you import.
Module authors can provide custom prompts for their schemas.
Currently, only ChatGPT is supported. You can use any of the
gpt-3.5 or gpt-4 models. The flag should match OpenAI API options.
Set OPENAI_API_KEY`

func init() {

ChatCmd.Flags().StringVarP(&(flags.ChatFlags.Model), "model", "M", "gpt-3.5-turbo", "LLM model to use [gpt-3.5-turbo,gpt-4]")
ChatCmd.Flags().StringVarP(&(flags.ChatFlags.Prompt), "prompt", "P", "", "path to the system prompt, the first message in the chat")
ChatCmd.Flags().StringVarP(&(flags.ChatFlags.Outfile), "outfile", "O", "", "path to write the output to")
}

func ChatRun(args []string) (err error) {

// you can safely comment this print out
// fmt.Println("not implemented")

err = cmd.Run(args, flags.RootPflags, flags.ChatFlags)

return err
}

var ChatCmd = &cobra.Command{

Use: "chat [args]",

Short: "Co-design with AI (alpha)",

Long: chatLong,

Run: func(cmd *cobra.Command, args []string) {

ga.SendCommandPath(cmd.CommandPath())

var err error

// Argument Parsing

err = ChatRun(args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
extra := func(cmd *cobra.Command) bool {

return false
}

ohelp := ChatCmd.HelpFunc()
ousage := ChatCmd.UsageFunc()

help := func(cmd *cobra.Command, args []string) {

ga.SendCommandPath(cmd.CommandPath() + " help")

if extra(cmd) {
return
}
ohelp(cmd, args)
}
usage := func(cmd *cobra.Command) error {
if extra(cmd) {
return nil
}
return ousage(cmd)
}

thelp := func(cmd *cobra.Command, args []string) {
help(cmd, args)
}
tusage := func(cmd *cobra.Command) error {
return usage(cmd)
}
ChatCmd.SetHelpFunc(thelp)
ChatCmd.SetUsageFunc(tusage)

}
1 change: 1 addition & 0 deletions cmd/hof/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func RootInit() {
RootCmd.AddCommand(FlowCmd)
RootCmd.AddCommand(FmtCmd)
RootCmd.AddCommand(ModCmd)
RootCmd.AddCommand(ChatCmd)
RootCmd.AddCommand(RunCmd)
RootCmd.AddCommand(FeedbackCmd)

Expand Down
9 changes: 9 additions & 0 deletions cmd/hof/flags/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package flags

type ChatFlagpole struct {
Model string
Prompt string
Outfile string
}

var ChatFlags ChatFlagpole
48 changes: 48 additions & 0 deletions design/cmds/chat.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmds

import (
"github.com/hofstadter-io/hofmod-cli/schema"
)

#ChatCommand: schema.#Command & {
Name: "chat"
Usage: "chat [args]"
Short: "Co-design with AI (alpha)"
Long: #ChatRootHelp

Flags: [...schema.#Flag] & [ {
Name: "model"
Type: "string"
Default: "\"gpt-3.5-turbo\""
Help: "LLM model to use [gpt-3.5-turbo,gpt-4]"
Long: "model"
Short: "M"
},
{
Name: "prompt"
Type: "string"
Default: "\"\""
Help: "path to the system prompt, the first message in the chat"
Long: "prompt"
Short: "P"
},
{
Name: "outfile"
Type: "string"
Default: "\"\""
Help: "path to write the output to"
Long: "outfile"
Short: "O"
},
]
}

#ChatRootHelp: #"""
Use chat to work with hof features or from modules you import.
Module authors can provide custom prompts for their schemas.
Currently, only ChatGPT is supported. You can use any of the
gpt-3.5 or gpt-4 models. The flag should match OpenAI API options.
Set OPENAI_API_KEY
"""#
1 change: 1 addition & 0 deletions design/main.cue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
cmds.#ModCommand,

// beta commands
cmds.#ChatCommand,
cmds.#RunCommand,

// additional commands
Expand Down
7 changes: 4 additions & 3 deletions docs/code/hof-schemas/dm/fields/common.cue
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ DataTypes: ID |
ID: UUID & {Default: "" | *"uuid_generate_v4()"}

Field: {
Name: string
Type: string
Reln?: string
Name: string
Plural: string | *"\(Name)s"
Type: string
Reln?: string
}

UUID: Field & {
Expand Down
61 changes: 61 additions & 0 deletions docs/code/hof-schemas/dm/sql/chat-etl.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package sql

import (
"strings"

"github.com/hofstadter-io/hof/schema/dm/fields"
)

ChatETL: {
// input models model fields value nested map (relns)
Original: Models: [string]: [string]: string | {[string]: string}
Datamodel: Models: {
for m, M in Original.Models {
(m): {
Name: m
Fields: {
for f, F in M {
// regular field
if !strings.HasPrefix(f, "$") {
(f): {
Name: f
[
if F == "string" {Varchar},
if F == "int" {fields.Int},
if F == "bool" {fields.Bool},
if F == "float" {fields.Float},
if F == "uuid" {fields.UUID},
if F == "datetime" {fields.Datetime},
if F == "email" {fields.Email},
if F == "password" {fields.Password},
if F == "url" {Varchar},
"UNKNOWN TYPE: \(F)" & false,
][0]
}
}

// $relations
if f == "$relations" {
for f2, F2 in F {
(f2): {
fields.UUID
Name: f2
Relation: {
Name: F2.name
Type: F2.type
Other: F2.model
}
}
}
}

// special fields
if f == "id" {
(f): SQL: PrimaryKey: true
}
}
}
}
}
}
}
3 changes: 2 additions & 1 deletion docs/code/hof-schemas/dm/sql/dm.cue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Model: M={
// $hof: History: ...

// for easy access
Name: M.$hof.metadata.name
Name: M.$hof.metadata.name
Plural: string | *"\(Name)s"

// These are the fields of a model
// they can map onto database columnts and form fields
Expand Down
9 changes: 7 additions & 2 deletions docs/code/hof-schemas/dm/sql/fields.cue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

CommonFields: {
ID: fields.UUID & {Default: "" | *"uuid_generate_v4()"}
ID: fields.UUID & {Default: string | *"uuid_generate_v4()"}
CreatedAt: fields.Datetime
UpdatedAt: fields.Datetime
}
Expand All @@ -14,6 +14,11 @@ SoftDelete: {
DeletedAt: fields.Datetime
}

PrimaryKey: fields.UUID & {
Default: string | *"uuid_generate_v4()"
SQL: PrimaryKey: true
}

Varchar: F=fields.String & {
sqlType: "varchar(\(F.Length))"
SQL: Type: "character varying(\(F.Length))"
}
Loading

0 comments on commit d11b312

Please sign in to comment.