Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added example of using assistant api #888

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}
Loading