49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package llm
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
// generateAnswer generates an answer for the given text using the OpenAI API
|
|
func GenerateJSON(ctx context.Context, client *openai.Client, model, text string, i interface{}) error {
|
|
req := openai.ChatCompletionRequest{
|
|
ResponseFormat: &openai.ChatCompletionResponseFormat{Type: openai.ChatCompletionResponseFormatTypeJSONObject},
|
|
Model: model,
|
|
Messages: []openai.ChatCompletionMessage{
|
|
{
|
|
|
|
Role: "user",
|
|
Content: text,
|
|
},
|
|
},
|
|
}
|
|
|
|
resp, err := client.CreateChatCompletion(ctx, req)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to generate answer: %v", err)
|
|
}
|
|
if len(resp.Choices) == 0 {
|
|
return fmt.Errorf("no response from OpenAI API")
|
|
}
|
|
|
|
fmt.Println(resp.Choices[0].Message.Content)
|
|
err = json.Unmarshal([]byte(resp.Choices[0].Message.Content), i)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GenerateJSONFromStruct(ctx context.Context, client *openai.Client, guidance, model string, i interface{}) error {
|
|
// TODO: use functions?
|
|
exampleJSON, err := json.Marshal(i)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return GenerateJSON(ctx, client, model, "Generate a character as JSON data. "+guidance+". This is the JSON fields that should contain: "+string(exampleJSON), i)
|
|
}
|