From 39470e46330f0e36cbb13f9f5ff789a6e8cc587f Mon Sep 17 00:00:00 2001 From: Ann Vasileva Date: Wed, 30 Oct 2024 00:32:42 +0300 Subject: [PATCH] added example of using assistant api --- README.md | 86 ++++++++++++++++++++++++++++++++++++++ examples/assistant/main.go | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 examples/assistant/main.go diff --git a/README.md b/README.md index 57d1d35b..b209ac87 100644 --- a/README.md +++ b/README.md @@ -807,6 +807,92 @@ func main() { } ``` + +
+Assistant API example + +```go +package main + +import ( + "context" + "fmt" + "log" + "os" + "time" + + "github.com/sashabaranov/go-openai" +) + +func main() { + ctx := context.Background() + client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) + + assistantName := "my assistant" + assistantRq := openai.AssistantRequest{ + Model: openai.GPT4o, + Name: &assistantName, + } + assistant, err := client.CreateAssistant(ctx, assistantRq) + if err != nil { + log.Fatal(err) + } + + threadRq := openai.ThreadRequest{} + threadRs, err := client.CreateThread(ctx, threadRq) + if err != nil { + log.Fatal(err) + } + + content := "Hello! Can you help me?" + + messageReq := openai.MessageRequest{ + Role: openai.ChatMessageRoleUser, + Content: content, + } + _, err = client.CreateMessage(ctx, threadRs.ID, messageReq) + if err != nil { + log.Fatal(err) + } + + runReq := openai.RunRequest{AssistantID: assistant.ID} + run, err := client.CreateRun(ctx, threadRs.ID, runReq) + if err != nil { + log.Fatal(err) + } + + isCompleted := false + + for !isCompleted { + resp, err := client.RetrieveRun(ctx, threadRs.ID, run.ID) + if err != nil { + log.Fatal(err) + } + + if resp.Status == openai.RunStatusFailed { + isCompleted = true + log.Fatal("Something went wrong. Openai response is failed:", resp.LastError.Message) + } + if resp.Status == openai.RunStatusCompleted { + isCompleted = true + asc := "asc" // get messages in ascending order (openai api doc) + limit := 10 + messages, err := client.ListMessage(ctx, threadRs.ID, &limit, &asc, nil, nil, &run.ID) + if err != nil { + log.Fatal(err) + } + + answer := messages.Messages[0].Content[0].Text.Value + + fmt.Print(answer) + } else { + time.Sleep(5 * time.Second) // wait 5 second before next request + } + } +} +``` +
+ See the `examples/` folder for more. ## Frequently Asked Questions diff --git a/examples/assistant/main.go b/examples/assistant/main.go new file mode 100644 index 00000000..a970c92d --- /dev/null +++ b/examples/assistant/main.go @@ -0,0 +1,78 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + "time" + + "github.com/sashabaranov/go-openai" +) + +func main() { + ctx := context.Background() + client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) + + assistantName := "my assistant" + assistantRq := openai.AssistantRequest{ + Model: openai.GPT4o, + Name: &assistantName, + } + assistant, err := client.CreateAssistant(ctx, assistantRq) + if err != nil { + log.Fatal(err) + } + + threadRq := openai.ThreadRequest{} + threadRs, err := client.CreateThread(ctx, threadRq) + if err != nil { + log.Fatal(err) + } + + content := "Hello! Can you help me?" + + messageReq := openai.MessageRequest{ + Role: openai.ChatMessageRoleUser, + Content: content, + } + _, err = client.CreateMessage(ctx, threadRs.ID, messageReq) + if err != nil { + log.Fatal(err) + } + + runReq := openai.RunRequest{AssistantID: assistant.ID} + run, err := client.CreateRun(ctx, threadRs.ID, runReq) + if err != nil { + log.Fatal(err) + } + + isCompleted := false + + for !isCompleted { + resp, err := client.RetrieveRun(ctx, threadRs.ID, run.ID) + if err != nil { + log.Fatal(err) + } + + if resp.Status == openai.RunStatusFailed { + isCompleted = true + log.Fatal("Something went wrong. Openai response is failed:", resp.LastError.Message) + } + if resp.Status == openai.RunStatusCompleted { + isCompleted = true + asc := "asc" // get messages in ascending order (openai api doc) + limit := 10 + messages, err := client.ListMessage(ctx, threadRs.ID, &limit, &asc, nil, nil, &run.ID) + if err != nil { + log.Fatal(err) + } + + answer := messages.Messages[0].Content[0].Text.Value + + fmt.Print(answer) + } else { + time.Sleep(5 * time.Second) // wait 5 second before next request + } + } +}