-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a167b2a
commit bcead82
Showing
8 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
github.com/eatmoreapple/openwechat v1.2.1 h1:ez4oqF/Y2NSEX/DbPV8lvj7JlfkYqvieeo4awx5lzfU= | ||
github.com/eatmoreapple/openwechat v1.2.1/go.mod h1:61HOzTyvLobGdgWhL68jfGNwTJEv0mhQ1miCXQrvWU8= | ||
github.com/eatmoreapple/openwechat v1.3.0 h1:HlAx7emjaTnvAJ+fcPSc3J3PyF/tULBDWk0KoizARs8= | ||
github.com/eatmoreapple/openwechat v1.3.0/go.mod h1:61HOzTyvLobGdgWhL68jfGNwTJEv0mhQ1miCXQrvWU8= | ||
github.com/eatmoreapple/openwechat v1.4.1 h1:hIVEr2Xaj+r1SXzdTigqhIXiuu6TZd+NPWdEVVt/qeM= | ||
github.com/eatmoreapple/openwechat v1.4.1/go.mod h1:ZxMcq7IpVWVU9JG7ERjExnm5M8/AQ6yZTtX30K3rwRQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package gpt35 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
const ModelGpt35Turbo = "gpt-3.5-turbo" | ||
|
||
const MaxTokensGpt35Turbo = 4096 | ||
|
||
const ( | ||
RoleUser RoleType = "user" | ||
RoleAssistant RoleType = "assistant" | ||
RoleSystem RoleType = "system" | ||
) | ||
|
||
const DefaultUrl = "https://api.openai.com/v1/chat/completions" | ||
|
||
type Client struct { | ||
transport *http.Client | ||
apiKey string | ||
url string | ||
} | ||
|
||
func NewClient(apiKey, proxy string) *Client { | ||
proxyUrl, err := url.Parse(proxy) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
transport := &http.Transport{ | ||
Proxy: http.ProxyURL(proxyUrl), | ||
} | ||
|
||
return &Client{ | ||
transport: &http.Client{ | ||
Transport: transport, | ||
Timeout: 110 * time.Second, | ||
}, | ||
apiKey: apiKey, | ||
url: DefaultUrl, | ||
} | ||
} | ||
|
||
func NewClientCustomUrl(apiKey string, url string) *Client { | ||
return &Client{ | ||
transport: http.DefaultClient, | ||
apiKey: apiKey, | ||
url: url, | ||
} | ||
} | ||
|
||
func (c *Client) GetChat(r *Request) (*Response, error) { | ||
jsonData, err := json.Marshal(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", c.url, bytes.NewBuffer(jsonData)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", "Bearer "+c.apiKey) | ||
|
||
httpResp, err := c.transport.Do(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
_ = httpResp.Body.Close() | ||
}() | ||
|
||
var resp Response | ||
err = json.NewDecoder(httpResp.Body).Decode(&resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package gpt35 | ||
|
||
type RoleType string | ||
type ModelType string | ||
|
||
type Request struct { | ||
Model ModelType `json:"model"` | ||
Messages []*Message `json:"messages"` | ||
Temperature float64 `json:"temperature,omitempty"` | ||
TopP float64 `json:"top_p,omitempty"` | ||
N int `json:"n,omitempty"` | ||
Stream bool `json:"stream,omitempty"` | ||
Stop interface{} `json:"stop,omitempty"` | ||
MaxTokens int `json:"max_tokens,omitempty"` | ||
PresencePenalty float64 `json:"presence_penalty,omitempty"` | ||
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"` | ||
LogitBias interface{} `json:"logit_bias,omitempty"` | ||
User string `json:"user,omitempty"` | ||
} | ||
|
||
type Response struct { | ||
ID string `json:"id"` | ||
Object string `json:"object"` | ||
Created int64 `json:"created"` | ||
Choices []*Choice `json:"choices"` | ||
Usage *Usage `json:"usage"` | ||
Error *Error `json:"error,omitempty"` | ||
} | ||
|
||
type Message struct { | ||
Role RoleType `json:"role,omitempty"` | ||
Content string `json:"content"` | ||
} | ||
|
||
type Choice struct { | ||
Index int `json:"index"` | ||
Message *Message `json:"message"` | ||
FinishReason string `json:"finish_reason"` | ||
} | ||
|
||
type Usage struct { | ||
PromptTokens int `json:"prompt_tokens"` | ||
CompletionTokens int `json:"completion_tokens"` | ||
TotalTokens int `json:"total_tokens"` | ||
} | ||
|
||
type Error struct { | ||
Message string `json:"message"` | ||
Type string `json:"type"` | ||
Param string `json:"param"` | ||
Code string `json:"code"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters