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

fix: add retry in client #25

Merged
merged 1 commit into from
Nov 19, 2023
Merged
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
4 changes: 1 addition & 3 deletions pkg/llm/client/openai/v1/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package v1

import (
"bytes"
"context"
"encoding/json"
"strings"
Expand Down Expand Up @@ -75,9 +74,8 @@ func (o *OpenAIV1) chat(ctx context.Context, prompt prompts.PromptTemplate, para
"presence_penalty": 0,
"n": 1,
}
postBody, _ := json.Marshal(data)

respBody, err := o.request(ctx, path, "POST", bytes.NewBuffer(postBody))
respBody, err := o.request(ctx, path, "POST", data)
if err != nil {
return nil, err
}
Expand Down
78 changes: 49 additions & 29 deletions pkg/llm/client/openai/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package v1

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"

"golang.org/x/time/rate"

Expand Down Expand Up @@ -63,34 +66,51 @@ func NewOpenAIV1(baseUrl, key string, qpm, burst int) *OpenAIV1 {

var _ llm.LLM = &OpenAIV1{}

func (o *OpenAIV1) request(ctx context.Context, path string, method string, body io.Reader) ([]byte, error) {
err := o.limiter.WaitN(ctx, 60)
if err != nil {
return nil, err
func (o *OpenAIV1) request(ctx context.Context, path string, method string, data map[string]any) ([]byte, error) {

jsonData, _ := json.Marshal(data)

maxRetry := 100
for i := 0; i < maxRetry; i++ {
body := bytes.NewBuffer(jsonData)
select {
case <-ctx.Done():
return nil, fmt.Errorf("openai request context cancelled")
default:
err := o.limiter.WaitN(ctx, 60)
if err != nil {
return nil, err
}

uri, err := url.JoinPath(o.baseUri, path)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, uri, body)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", o.key))

o.log.Debugf("request: %s", uri)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
o.log.Errorf("fail to call openai, sleep 30s and retry. err: %v", err)
time.Sleep(time.Second * 30)
continue
}

// Read Response Body
respBody, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close()

if resp.StatusCode != 200 {
o.log.Errorf("fail to call openai, sleep 30s and retry. status code error: %d, resp body: %s", resp.StatusCode, string(respBody))
time.Sleep(time.Second * 30)
continue
}
//o.log.Debugf("response: %s", respBody)
return respBody, nil
}
}

uri, err := url.JoinPath(o.baseUri, path)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, uri, body)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", o.key))

o.log.Debugf("request: %s", uri)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

// Read Response Body
respBody, _ := io.ReadAll(resp.Body)

if resp.StatusCode != 200 {
return nil, fmt.Errorf("fail to call openai, status code error: %d, resp body: %s", resp.StatusCode, string(respBody))
}
//o.log.Debugf("response: %s", respBody)
return respBody, nil
return nil, fmt.Errorf("fail to call openai after retry 100 times")
}
4 changes: 1 addition & 3 deletions pkg/llm/client/openai/v1/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package v1

import (
"bytes"
"context"
"encoding/json"
"strings"
Expand Down Expand Up @@ -77,9 +76,8 @@ func (o *OpenAIV1) completion(ctx context.Context, prompt prompts.PromptTemplate
"n": 1,
"best_of": 1,
}
postBody, _ := json.Marshal(data)

respBody, err := o.request(ctx, path, "POST", bytes.NewBuffer(postBody))
respBody, err := o.request(ctx, path, "POST", data)
if err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/llm/client/openai/v1/embedding.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package v1

import (
"bytes"
"context"
"encoding/json"
"strings"
Expand Down Expand Up @@ -64,13 +63,12 @@ func (o *OpenAIV1) embedding(ctx context.Context, doc string) (*EmbeddingResult,
path := "v1/embeddings"

model := "text-embedding-ada-002"
data := map[string]string{
data := map[string]interface{}{
"model": model,
"input": doc,
}
postBody, _ := json.Marshal(data)

respBody, err := o.request(ctx, path, "POST", bytes.NewBuffer(postBody))
respBody, err := o.request(ctx, path, "POST", data)
if err != nil {
return nil, err
}
Expand Down
Loading