Created
January 9, 2025 17:55
-
-
Save wangjohn/8f0658898597924119efd96a1ebc6951 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ( | |
| "github.com/sashabaranov/go-openai" | |
| "github.com/sashabaranov/go-openai/jsonschema" | |
| ) | |
| type SupportResponse struct { | |
| Answer string `json:"answer"` | |
| RelatedDocs []string `json:"related_docs"` | |
| } | |
| func GetSupportResponse(messages []openai.ChatCompletionMessage) (*SupportResponse, error) { | |
| var supportResponse SupportResponse | |
| schema, err := jsonschema.GenerateSchemaForType(supportResponse) | |
| if err != nil { | |
| return nil, err | |
| } | |
| resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ | |
| Messages: messages, | |
| ResponseFormat: &openai.ChatCompletionResponseFormat{ | |
| Type: openai.ChatCompletionResponseFormatTypeJSONSchema, | |
| JSONSchema: &openai.ChatCompletionResponseFormatJSONSchema{ | |
| Name: "support_response", | |
| Schema: schema, | |
| Strict: true, | |
| }, | |
| }, | |
| }) | |
| if err != nil { | |
| return nil, err | |
| } | |
| err = schema.Unmarshal(resp.Choices[0].Message.Content, &supportResponse) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return &supportResponse, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment