add job/queue logics

This commit is contained in:
mudler
2024-01-21 16:12:34 +01:00
parent d52e3b8309
commit 61e4be0d0c
7 changed files with 236 additions and 41 deletions

View File

@@ -9,7 +9,7 @@ import (
)
// generateAnswer generates an answer for the given text using the OpenAI API
func GenerateJSON(client *openai.Client, model, text string, i interface{}) error {
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,
@@ -22,7 +22,7 @@ func GenerateJSON(client *openai.Client, model, text string, i interface{}) erro
},
}
resp, err := client.CreateChatCompletion(context.Background(), req)
resp, err := client.CreateChatCompletion(ctx, req)
if err != nil {
return fmt.Errorf("failed to generate answer: %v", err)
}
@@ -37,11 +37,11 @@ func GenerateJSON(client *openai.Client, model, text string, i interface{}) erro
return nil
}
func GenerateJSONFromStruct(client *openai.Client, guidance, model string, i interface{}) error {
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(client, model, "Generate a character as JSON data. "+guidance+". This is the JSON fields that should contain: "+string(exampleJSON), i)
return GenerateJSON(ctx, client, model, "Generate a character as JSON data. "+guidance+". This is the JSON fields that should contain: "+string(exampleJSON), i)
}