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

Add example for assistant invocation to readme #639

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,79 @@ func main() {
```
</details>

<details>
<summary>Assistant Invocation</summary>

```go
package main

import (
"context"
"fmt"
"time"

"github.com/sashabaranov/go-openai"
)

func main() {
client := openai.NewClient("your token")
ctx := context.Background()

thread, err := client.CreateThread(ctx, openai.ThreadRequest{})
if err != nil {
fmt.Printf("CreateThread error: %v\n", err)
return
}

messageReq := openai.MessageRequest{
Role: openai.ChatMessageRoleUser,
Content: "Lorem ipsum",
}
_, err = client.CreateMessage(ctx, thread.ID, messageReq)
if err != nil {
fmt.Printf("CreateMessage error: %v\n", err)
return
}

runReq := openai.RunRequest{
AssistantID: "your assistant id",
}
run, err := client.CreateRun(ctx, thread.ID, runReq)
if err != nil {
fmt.Printf("CreateRun error: %v\n", err)
return
}

// Poll for a status that indicates run has finished
for run.Status == openai.RunStatusQueued || run.Status == openai.RunStatusInProgress {
run, err = client.RetrieveRun(ctx, run.ThreadID, run.ID)
if err != nil {
fmt.Printf("RetrieveRun error: %v\n", err)
return
}
time.Sleep(100 * time.Millisecond)
}
if run.Status != openai.RunStatusCompleted {
fmt.Printf("Run finised with status: %v\n", run.Status)
if run.Status == openai.RunStatusFailed {
fmt.Printf("Run error: [%v] %v\n", run.LastError.Code, run.LastError.Message)
}
return
}

// Get only latest message
numMessages := 1
messages, err := client.ListMessage(ctx, run.ThreadID, &numMessages, nil, nil, nil)
if err != nil {
fmt.Printf("RetrieveRun error: %v\n", err)
return
}

fmt.Printf(messages.Messages[0].Content[0].Text.Value)
}
```
</details>

<details>
<summary>Audio Speech-To-Text</summary>

Expand Down
Loading