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

How to set Stream in CreateRun() of Thread in Assistant instead of returning all results at once? #642

Open
WangQuanTang opened this issue Jan 24, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@WangQuanTang
Copy link

WangQuanTang commented Jan 24, 2024

Is your feature request related to a problem? Please describe.
According to the code of this example, everything can be run including creating Assistant and Thread. But there is a problem, maxTokens and Stream cannot be set. This results in a long wait for a response to be returned.After I checked the source code, I found that the above configuration information is not supported.

Describe the solution you'd like
Are there any plans to add configuration information to client.CreateRun() in the future? Or in the current situation, what can I do to return the request results multiple times?Please ignore my poor English!Thank you!

example code

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: assistantId,
	}
	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
	}
        // When the text message is very long, the response time becomes very slow
	fmt.Printf(messages.Messages[0].Content[0].Text.Value)
}
@WangQuanTang WangQuanTang added the enhancement New feature or request label Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant