Skip to content

Commit

Permalink
added example of using assistant api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ann Vasileva committed Oct 29, 2024
1 parent d10f1b8 commit 39470e4
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,92 @@ func main() {
}
```
</details>

<details>
<summary>Assistant API example</summary>

```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
}
}
}
```
</details>

See the `examples/` folder for more.

## Frequently Asked Questions
Expand Down
78 changes: 78 additions & 0 deletions examples/assistant/main.go
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 39470e4

Please sign in to comment.