我心飞呀飞 发表于 2025-2-19 12:27:24

找gpt写了一个go的后端 来用老马的xAi

150刀自用还是不错的,翻译啥的。<br />
有几个坑,不支持内地、香港的主机,地区不可用,我找了个日本的可以用。<br />
<img id="aimg_Oi0Lr" onclick="zoom(this, this.src, 0, 0, 0)" class="zoom" src="https://img.picui.cn/free/2025/02/19/67b55d49a61b0.png" onmouseover="img_onmouseoverfunc(this)" onload="thumbImg(this)" border="0" alt="" /><br />
<br />
<br />
<br />
运行后通过post http://服务器地址:8089/ask<br />
body格式<br />
{<br />
&quot;prompt&quot;: &quot;AdGuard 9设备终身价格仅需16刀折合人民币116元&quot;<br />
}<br />
<br />
<br />
go写的,自行替换key编译一下就能用,端口和细节按需修改。<br />
#main.go 源代码<br /><div class="blockcode"><div id="code_Ic9"><ol><li>package main<br /><li><br /><li>import (<br /><li>    &quot;bytes&quot;<br /><li>    &quot;encoding/json&quot;<br /><li>    &quot;fmt&quot;<br /><li>    &quot;io&quot;<br /><li>    &quot;net/http&quot;<br /><li>    &quot;time&quot;<br /><li>)<br /><li><br /><li>// xAiRequest 定义请求结构体<br /><li>type xAiRequest struct {<br /><li>    Prompt string `json:&quot;prompt&quot;`<br /><li>}<br /><li><br /><li>// xAiResponse 定义简化后的响应结构体<br /><li>type xAiResponse struct {<br /><li>    Content string `json:&quot;content&quot;`<br /><li>}<br /><li><br /><li>// Message 定义消息结构体<br /><li>type Message struct {<br /><li>    Rolestring `json:&quot;role&quot;`<br /><li>    Content string `json:&quot;content&quot;`<br /><li>}<br /><li><br /><li>// APIRequest 定义API请求结构体<br /><li>type APIRequest struct {<br /><li>    Messages []Message `json:&quot;messages&quot;`<br /><li>    Modelstring`json:&quot;model&quot;`<br /><li>}<br /><li><br /><li>func main() {<br /><li>    http.HandleFunc(&quot;/ask&quot;, func(w http.ResponseWriter, r *http.Request) {<br /><li>      // 解析请求体<br /><li>      var req xAiRequest<br /><li>      if err := json.NewDecoder(r.Body).Decode(&amp;req); err != nil {<br /><li>            http.Error(w, fmt.Sprintf(&quot;解析请求失败: %v&quot;, err), http.StatusBadRequest)<br /><li>            return<br /><li>      }<br /><li><br /><li>      // 调用xAi API<br /><li>      xAiResponse, err := callxAiAPI(req.Prompt)<br /><li>      if err != nil {<br /><li>            http.Error(w, fmt.Sprintf(&quot;调用API失败: %v&quot;, err), http.StatusInternalServerError)<br /><li>            return<br /><li>      }<br /><li><br /><li>      // 返回响应<br /><li>      w.Header().Set(&quot;Content-Type&quot;, &quot;application/json&quot;)<br /><li>      if err := json.NewEncoder(w).Encode(xAiResponse); err != nil {<br /><li>            http.Error(w, fmt.Sprintf(&quot;编码响应失败: %v&quot;, err), http.StatusInternalServerError)<br /><li>            return<br /><li>      }<br /><li>    })<br /><li><br /><li>    // 启动HTTP服务器<br /><li>    server := &amp;http.Server{<br /><li>      Addr:   &quot;:8089&quot;,<br /><li>      ReadTimeout:10 * time.Second,<br /><li>      WriteTimeout: 10 * time.Second,<br /><li>    }<br /><li>    fmt.Println(&quot;服务器正在运行,监听端口 8089...&quot;)<br /><li>    if err := server.ListenAndServe(); err != nil {<br /><li>      fmt.Printf(&quot;服务器启动失败: %v\n&quot;, err)<br /><li>    }<br /><li>}<br /><li><br /><li>// callxAiAPI 调用xAi API<br /><li>func callxAiAPI(prompt string) (*xAiResponse, error) {<br /><li>    url := &quot;https://api.x.ai/v1/chat/completions&quot;<br /><li><br /><li>    // 创建请求体<br /><li>    requestBody, err := json.Marshal(APIRequest{<br /><li>      Messages: []Message{<br /><li>            {<br /><li>                Role:&quot;user&quot;, // 角色为用户<br /><li>                Content: prompt, // 用户输入的内容<br /><li>            },<br /><li>      },<br /><li>      Model: &quot;grok-2-latest&quot;, // 模型名称<br /><li>    })<br /><li>    if err != nil {<br /><li>      return nil, fmt.Errorf(&quot;创建请求体失败: %v&quot;, err)<br /><li>    }<br /><li><br /><li>    // 创建HTTP请求<br /><li>    req, err := http.NewRequest(&quot;POST&quot;, url, bytes.NewBuffer(requestBody))<br /><li>    if err != nil {<br /><li>      return nil, fmt.Errorf(&quot;创建HTTP请求失败: %v&quot;, err)<br /><li>    }<br /><li><br /><li>    // 添加Authorization头<br /><li>    req.Header.Set(&quot;Authorization&quot;, &quot;Bearer 改成你的key&quot;)<br /><li>    req.Header.Set(&quot;Content-Type&quot;, &quot;application/json&quot;)<br /><li><br /><li>    // 发送请求<br /><li>    client := &amp;http.Client{<br /><li>      Timeout: 10 * time.Second, // 设置超时时间<br /><li>    }<br /><li>    resp, err := client.Do(req)<br /><li>    if err != nil {<br /><li>      return nil, fmt.Errorf(&quot;发送请求失败: %v&quot;, err)<br /><li>    }<br /><li>    defer resp.Body.Close()<br /><li><br /><li>    // 读取响应体<br /><li>    body, err := io.ReadAll(resp.Body)<br /><li>    if err != nil {<br /><li>      return nil, fmt.Errorf(&quot;读取响应体失败: %v&quot;, err)<br /><li>    }<br /><li>    <br /><li>    // 打印 HTTP 状态码和响应体<br /><li>    fmt.Printf(&quot;HTTP Status Code: %d\n&quot;, resp.StatusCode)<br /><li>    fmt.Printf(&quot;Response Body: %s\n&quot;, string(body))<br /><li><br /><li>    // 检查响应状态码<br /><li>    if resp.StatusCode != http.StatusOK {<br /><li>      return nil, fmt.Errorf(&quot;API请求失败,状态码: %d, 响应: %s&quot;, resp.StatusCode, string(body))<br /><li>    }<br /><li><br /><li>    // 解析响应体<br /><li>    var apiResponse struct {<br /><li>      Choices []struct {<br /><li>            Message struct {<br /><li>                Content string `json:&quot;content&quot;`<br /><li>            } `json:&quot;message&quot;`<br /><li>      } `json:&quot;choices&quot;`<br /><li>    }<br /><li>    if err := json.Unmarshal(body, &amp;apiResponse); err != nil {<br /><li>      return nil, fmt.Errorf(&quot;解析响应体失败: %v&quot;, err)<br /><li>    }<br /><li><br /><li>    // 提取并返回content字段<br /><li>    if len(apiResponse.Choices) &gt; 0 {<br /><li>      return &amp;xAiResponse{<br /><li>            Content: apiResponse.Choices.Message.Content,<br /><li>      }, nil<br /><li>    }<br /><li><br /><li>    return nil, fmt.Errorf(&quot;API响应格式错误,没有找到content&quot;)<br /><li>}<br /><li></ol></div><em onclick="copycode($('code_Ic9'));">复制代码</em></div>

801N 发表于 2025-2-19 13:42:30

有没有可能不知道怎么编译,不知道怎么运行,只知道类似php的放目录里面就行

我心飞呀飞 发表于 2025-2-19 13:49:36

<i class="pstatus"> 本帖最后由 我心飞呀飞 于 2025-2-19 13:51 编辑 </i><br />
<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16241803&ptid=1390214" target="_blank"><font color="#999999">801N 发表于 2025-2-19 13:42</font></a></font><br />
有没有可能不知道怎么编译,不知道怎么运行,只知道类似php的放目录里面就行 ...</blockquote></div><br />
<br />
PHP的<br />
<br />
代码过不了waf自行复制吧<br />
https://codecopy.cn/post/h0fpoy<br />
<br />
<strong>解释:</strong><br />
类定义:为了模拟 Go 中的结构体,我们在 PHP 中定义了 XAiRequest, XAiResponse, Message, 和 APIRequest 类。<br />
请求处理:handleRequest 函数处理 POST 请求,解析请求体并调用 xAi API。<br />
调用 xAi API:在 callXAiAPI 函数中,我们使用 file_get_contents 来发送 HTTP 请求并读取响应。请求头包含 Authorization 和 Content-Type。<br />
服务器监听:PHP 在标准 HTTP 服务器环境下(如 php -S 启动)监听指定端口和路径。<br />
<strong>运行方法:</strong><br />
将此 PHP 文件保存为 index.php。<br />
启动 PHP 内置服务器:<br />
php -S 0.0.0.0:8089<br />
通过 HTTP POST 请求访问 http://localhost:8089/ask,提交 JSON 请求体。<br />
请确保您的 PHP 环境支持 file_get_contents 和 json_decode,这些是处理请求和响应的关键功能。<br />
<br />
如果您有更多需求或修改,随时告诉我!

pykane 发表于 2025-2-19 13:42:00

这么筒单的一个功能, 为啥味不用Python写?几行就行。<br />
看这Go 代码行数就头疼。<br />

我心飞呀飞 发表于 2025-2-19 14:01:15

<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16241846&ptid=1390214" target="_blank"><font color="#999999">pykane 发表于 2025-2-19 14:01</font></a></font><br />
这么筒单的一个功能, 为啥味不用Python写?几行就行。<br />
看这Go 代码行数就头疼。<br />
...</blockquote></div><br />
没py环境

我心飞呀飞 发表于 2025-2-19 14:01:44

再弄个捷径去post中转的接口在ios手机上也能用
页: [1]
查看完整版本: 找gpt写了一个go的后端 来用老马的xAi