-
Notifications
You must be signed in to change notification settings - Fork 9
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
Showing
14 changed files
with
299 additions
and
118 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
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,5 @@ | ||
package third_party | ||
|
||
type AssistantService interface { | ||
ProcessText(fromUserName, toUserName, content string) interface{} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,89 @@ | ||
package dashscope | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"weChatRobot-go/pkg/logger" | ||
) | ||
|
||
const generationUrl = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation" | ||
|
||
type Client struct { | ||
apiKey string | ||
httpClient *http.Client | ||
} | ||
|
||
type GenerationParam struct { | ||
Model string `json:"model"` | ||
Input Input `json:"input"` | ||
} | ||
|
||
type Input struct { | ||
Messages []Message `json:"messages"` | ||
} | ||
|
||
type Message struct { | ||
Role string `json:"role"` | ||
Content string `json:"content"` | ||
} | ||
|
||
type GenerationResult struct { | ||
Output struct { | ||
Text string `json:"text"` | ||
FinishReason string `json:"finish_reason"` | ||
} `json:"output"` | ||
} | ||
|
||
func (client *Client) call(param *GenerationParam) (*GenerationResult, error) { | ||
// 转换为JSON格式 | ||
payload, err := json.Marshal(param) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 构建请求 | ||
req, err := http.NewRequest("POST", generationUrl, bytes.NewBuffer(payload)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 设置请求头部 | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", client.apiKey)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := client.httpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
var errorResponse struct { | ||
Code string `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&errorResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil, fmt.Errorf("API error: %s - %s", errorResponse.Code, errorResponse.Message) | ||
} | ||
|
||
result := &GenerationResult{} | ||
var respBytes []byte | ||
if respBytes, err = io.ReadAll(resp.Body); err != nil { | ||
logger.Error(err, "读取通义千问响应内容报错") | ||
return nil, nil | ||
} | ||
err = json.Unmarshal(respBytes, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, 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 dashscope | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"weChatRobot-go/pkg/logger" | ||
"weChatRobot-go/pkg/util" | ||
) | ||
|
||
type Dashscope struct { | ||
client *Client | ||
} | ||
|
||
func NewDashscope() *Dashscope { | ||
apiKey := os.Getenv("DASHSCOPE_API_KEY") | ||
var client *Client | ||
if apiKey != "" { | ||
client = &Client{ | ||
apiKey: apiKey, | ||
httpClient: &http.Client{}, | ||
} | ||
} | ||
|
||
return &Dashscope{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (d *Dashscope) ProcessText(fromUserName, toUserName, content string) interface{} { | ||
if d.client == nil { | ||
return nil | ||
} | ||
|
||
param := &GenerationParam{ | ||
Model: "qwen-turbo", | ||
Input: Input{ | ||
Messages: []Message{ | ||
{Role: "system", Content: "你是一个AI助手,保持回复内容尽量简短"}, | ||
{Role: "user", Content: content}, | ||
}, | ||
}, | ||
} | ||
|
||
var result *GenerationResult | ||
var err error | ||
if result, err = d.client.call(param); err != nil { | ||
logger.Error(err, "dashscope generation error") | ||
return nil | ||
} | ||
|
||
return util.BuildRespTextMessage(fromUserName, toUserName, result.Output.Text) | ||
} |
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,13 @@ | ||
package dashscope | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"weChatRobot-go/pkg/util" | ||
) | ||
|
||
func TestGetRespMessage(t *testing.T) { | ||
dashscope := NewDashscope() | ||
var respMessage = dashscope.ProcessText("aaa", "bbb", "什么是GPT") | ||
fmt.Printf("respMessage:%v", util.ToJsonString(respMessage)) | ||
} |
Oops, something went wrong.