Skip to content

Commit

Permalink
Add response format in profile to support json mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kznrluk committed Nov 8, 2023
1 parent 57f25b8 commit 0590140
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
5 changes: 3 additions & 2 deletions chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func Rest(ctx context.Context, oc *openai.Client, conv conv.Conversation) (strin
openai.ChatCompletionRequest{
Model: profile.Model,
Messages: conv.ToChatCompletionMessage(),
ResponseFormat: profile.GetResponseFormat(),
MaxTokens: customParams.MaxTokens,
Temperature: customParams.Temperature,
TopP: customParams.TopP,
Expand All @@ -124,6 +125,7 @@ func Stream(ctx context.Context, oc *openai.Client, conv conv.Conversation) (str
openai.ChatCompletionRequest{
Model: profile.Model,
Messages: conv.ToChatCompletionMessage(),
ResponseFormat: profile.GetResponseFormat(),
MaxTokens: customParams.MaxTokens,
Temperature: customParams.Temperature,
TopP: customParams.TopP,
Expand All @@ -138,7 +140,6 @@ func Stream(ctx context.Context, oc *openai.Client, conv conv.Conversation) (str
return "", err
}

fmt.Printf("\n")
data := ""
for {
resp, err := stream.Recv()
Expand All @@ -165,8 +166,8 @@ func createCancellableContext() (context.Context, context.CancelFunc) {

select {
case <-sigChan:
cancel()
println()
cancel()
case <-ctx.Done():
}

Expand Down
33 changes: 23 additions & 10 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ type Profile struct {
UserName string `yaml:"UserName"`
AutoSave bool `yaml:"AutoSave"`
Summarize bool `yaml:"Summarize"`
ResponseFormat string `yaml:"ResponseFormat"`
SystemContext string `yaml:"SystemContext"`
Messages []PreMessage `yaml:"Messages"`
CustomParameters CustomParameters `yaml:"CustomParameters,omitempty"`
}

func (p Profile) GetResponseFormat() *openai.ChatCompletionResponseFormat {
return &openai.ChatCompletionResponseFormat{
Type: openai.ChatCompletionResponseFormatType(p.ResponseFormat),
}
}

type PreMessage struct {
Role string `yaml:"Role"`
Content string `yaml:"Content"`
Expand Down Expand Up @@ -149,13 +156,14 @@ func InitialProfile() Profile {
currentUser.Username = "aski"
}
return Profile{
ProfileName: "GPT4",
UserName: currentUser.Username,
AutoSave: true,
Summarize: true,
SystemContext: "You are a kind and helpful chat AI. Sometimes you may say things that are incorrect, but that is unavoidable.",
Model: openai.GPT4,
Messages: []PreMessage{},
ProfileName: "GPT4",
UserName: currentUser.Username,
AutoSave: true,
Summarize: false,
ResponseFormat: string(openai.ChatCompletionResponseFormatTypeText),
SystemContext: "You are a kind and helpful chat AI. Sometimes you may say things that are incorrect, but that is unavoidable.",
Model: openai.GPT4,
Messages: []PreMessage{},
}
}

Expand All @@ -170,18 +178,23 @@ func validateProfile(profile Profile) error {
return fmt.Errorf("SystemContext must not be empty")
}
if profile.Model == "" {
return fmt.Errorf("Model must not be empty")
return fmt.Errorf("model must not be empty")
}

for _, message := range profile.Messages {
if message.Role == "" {
return fmt.Errorf("Message Role must not be empty")
return fmt.Errorf("message Role must not be empty")
}
if message.Content == "" {
return fmt.Errorf("Message Content must not be empty")
return fmt.Errorf("message Content must not be empty")
}
}

if profile.ResponseFormat != string(openai.ChatCompletionResponseFormatTypeJSONObject) &&
profile.ResponseFormat != string(openai.ChatCompletionResponseFormatTypeText) {
return fmt.Errorf("response_format must be either json_object or text")
}

return ValidateCustomParameters(profile.CustomParameters)
}

Expand Down
4 changes: 3 additions & 1 deletion lib/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func StartDialog(cfg config.Config, cv conv.Conversation, isRestMode bool, resto

first := !restored
for {
fmt.Printf("\n")
editor.PromptWriter = func(w io.Writer) (int, error) {
return io.WriteString(w, fmt.Sprintf("%.*s > ", 6, cv.Last().Sha1))
}
Expand Down Expand Up @@ -95,9 +96,10 @@ func StartDialog(cfg config.Config, cv conv.Conversation, isRestMode bool, resto
showPendingHeader(openai.ChatMessageRoleAssistant, lastMessage)
}

fmt.Printf("\n")
data, err := chat.RetrieveResponse(isRestMode, cfg, cv)
if err != nil {
fmt.Printf(err.Error())
fmt.Printf("\n%s", err.Error())
continue
}

Expand Down

0 comments on commit 0590140

Please sign in to comment.