How to use assistant? #632
Unanswered
flocko-motion
asked this question in
Q&A
Replies: 1 comment 2 replies
-
I got it working by doing something like this. The code is not very nice, sorry! I wrote it very quickly! package main
import (
"context"
"log"
"os"
"time"
"github.com/sashabaranov/go-openai"
)
const (
assistantId = "assistant_id"
prompt = "my prompt"
)
func main() {
ctx := context.Background()
client := openai.NewClient(os.Getenv("OPENAI_API_TOKEN"))
run, err := client.CreateThreadAndRun(ctx, openai.CreateThreadAndRunRequest{
RunRequest: openai.RunRequest{AssistantID: assistantId},
Thread: openai.ThreadRequest{
Messages: []openai.ThreadMessage{{
Role: openai.ChatMessageRoleUser,
Content: prompt,
}},
},
})
if err != nil {
log.Fatal(err)
}
for run.Status == openai.RunStatusQueued || run.Status == openai.RunStatusInProgress {
run, err = client.RetrieveRun(ctx, run.ThreadID, run.ID)
if err != nil {
log.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
}
if run.Status != openai.RunStatusCompleted {
log.Fatalf("run failed with status %s", run.Status)
}
numMessages := 1
messages, err := client.ListMessage(ctx, run.ThreadID, &numMessages, nil, nil, nil)
if err != nil {
log.Fatal(err)
}
log.Printf(messages.Messages[0].Content[0].Text.Value)
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
There's no example for using assistants, yet - so I tried to find out myself from the source code and the openAI docs.
I figured out, how to create an assistant, create a thread, attach a message, run the assistant and wait for it to complete ... But I couldn't figure out, how to fetch the generated output.
Here's what I got so far:
Beta Was this translation helpful? Give feedback.
All reactions