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

CompletionBatchingRequestSupport #220

Merged
merged 10 commits into from
Apr 3, 2023

Conversation

NullpointerW
Copy link
Contributor

solve iusse #201 i think this feature is useful
i closed pr #210 because I think there are too many unnecessary changes, here is a more concise change to implement

c := openai.NewClient("your token")
	ctx := context.Background()

	
	// test CreateCompletion
	reqNor := CompletionRequest{
		Prompt:    []string{"Lorem ipsum", "Lorem ipsum", "Lorem ipsum"}, //use string or []string 
		Model:     GPT3Curie,
		MaxTokens: 5,
	}
	resp, err := c.CreateCompletion(ctx, reqNor)
	checks.NoError(t, err, "CreateCompletion returned error")
	if err == nil {
		for _, c := range resp.Choices {
			t.Logf("index =`%d`,text is `%s` \n", c.Index, c.Text)
		}
	}

Prompt can be assigned string or []string type
Streaming is also supported

c := openai.NewClient("your token")
	ctx := context.Background()

	// test CreateCompletionStream
	streq := CompletionRequest{
		Model:     GPT3Curie,
		MaxTokens: 5,
		Prompt:   []string{ "a dog entered the alley", "a dog entered the alley", "a dog entered the alley"},
		Stream:    true,
	}
	stream, err := c.CreateCompletionStream(ctx, streq)
	if err != nil {
		t.Logf("CompletionStream error: %v\n", err)
		return
	}
	
	defer stream.Close()
	type index = int
	choicesMap := make(map[index]string)
	for {
		response, streamErr := stream.Recv()
		if errors.Is(streamErr, io.EOF) {
			t.Logf("CompletionBatch Stream finished")
			break
		}

		if streamErr != nil {
			checks.NoError(t, streamErr, "CompletionBatchStream error:Stream error")
		}
		t.Logf("Stream response: %#+v\n", response.Choices)
		choicesMap[response.Choices[0].Index] += response.Choices[0].Text
	}
	for k, v := range choicesMap {
		t.Logf("choice_index:%d,text:%s", k, v)
	}

@codecov
Copy link

codecov bot commented Apr 2, 2023

Codecov Report

Merging #220 (d539b60) into master (b542086) will increase coverage by 0.56%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #220      +/-   ##
==========================================
+ Coverage   71.94%   72.51%   +0.56%     
==========================================
  Files          21       21              
  Lines         581      593      +12     
==========================================
+ Hits          418      430      +12     
  Misses        124      124              
  Partials       39       39              
Impacted Files Coverage Δ
completion.go 100.00% <100.00%> (ø)
stream.go 79.31% <100.00%> (+3.31%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

completion.go Outdated Show resolved Hide resolved
Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the PR! That was a long-standing issue 🙌🏻

completion.go Outdated Show resolved Hide resolved
@sashabaranov sashabaranov merged commit bee0656 into sashabaranov:master Apr 3, 2023
@sashabaranov sashabaranov mentioned this pull request Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants