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

windows11系统 运行到 2023/03/07 13:24:16 chatgpt.go:37: 正在调用OpenAI API生成文本... 北京 然后就关闭了 #2

Closed
ghost opened this issue Mar 7, 2023 · 6 comments

Comments

@ghost
Copy link

ghost commented Mar 7, 2023

windows11系统
运行到
2023/03/07 13:24:16 chatgpt.go:37: 正在调用OpenAI API生成文本... 北京
然后就关闭了

@SuInk
Copy link
Owner

SuInk commented Mar 7, 2023

api.openai.com在中国大陆已不能访问。。

@ghost
Copy link
Author

ghost commented Mar 7, 2023

开全局代理也不行(哭)

@SuInk
Copy link
Owner

SuInk commented Mar 7, 2023

开全局代理也不行(哭)

程序默认不走代理
你会用go语言的话
可尝试把/cmd/chatgpt/chatgpt.go 47行改为下面代码,7890为clash默认代理端口。

client := http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyURL("http://127.0.0.1:7890"),
		},
	}
resp, err := client.Do(req)

然后 运行
cd QQ-ChatGPT-Bot
go run main.go

@ghost
Copy link
Author

ghost commented Mar 7, 2023

懂了 , 这就去改 , up真是太好了

@Traveler07
Copy link

Traveler07 commented Mar 8, 2023

修改/cmd/chatgpt/chatgpt.go为以下内容,可以让程序走http://127.0.0.1:7890,也就是clash的默认端口。虽然我不会go,但ChatGPT会啊,2333

package chatgpt

import (
	"QQ-ChatGPT-Bot/config"
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"net/url"
	"strings"
)

const OpenaiApiUrl = "https://api.openai.com/v1/chat/completions"

type OpenAiRcv struct {
	Id      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	Model   string `json:"model"`
	Choices []struct {
		Message struct {
			Role    string `json:"role"`
			Content string `json:"content"`
		} `json:"message"`
		FinishReason string `json:"finish_reason"`
	} `json:"choices"`
	Usage struct {
		PromptTokens    int `json:"prompt_tokens"`
		CompletionTokes int `json:"completion_tokens"`
		TotalTokens     int `json:"total_tokens"`
	}
}

// GenerateText 调用openai的API生成文本
func GenerateText(text string) string {
	log.Println("正在调用OpenAI API生成文本...", text)
	proxyURL, err := url.Parse("http://127.0.0.1:7890")
	if err != nil {
		log.Println(err)
	}
	httpClient := &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyURL(proxyURL),
		},
	}
	postData := []byte(fmt.Sprintf(`{
	  "model": "%s",
	  "messages": %s,
	  "max_tokens": %d,
	  "temperature": %.1f
	}`, config.Cfg.OpenAi.Model, "[{\"role\": \"user\", \"content\": \""+text+"\"}]", config.Cfg.OpenAi.MaxTokens, config.Cfg.OpenAi.Temperature))
	req, _ := http.NewRequest("POST", OpenaiApiUrl, bytes.NewBuffer(postData))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer "+config.Cfg.OpenAi.ApiKey)
	resp, err := httpClient.Do(req)
	if err != nil {
		log.Println(err)
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	var openAiRcv OpenAiRcv
	err = json.Unmarshal(body, &openAiRcv)
	if err != nil {
		log.Println(err)
	}
	if len(openAiRcv.Choices) == 0 {
		log.Println("OpenAI API调用失败,返回内容:", string(body))
		return string(body)
	}
	openAiRcv.Choices[0].Message.Content = strings.ReplaceAll(openAiRcv.Choices[0].Message.Content, "\n\n", "")
	log.Printf("Model: %s TotalTokens: %d+%d=%d", openAiRcv.Model, openAiRcv.Usage.PromptTokens, openAiRcv.Usage.CompletionTokes, openAiRcv.Usage.TotalTokens)
	return openAiRcv.Choices[0].Message.Content
}

```

@SuInk
Copy link
Owner

SuInk commented Mar 8, 2023

修改/cmd/chatgpt/chatgpt.go为以下内容,可以让程序走http://127.0.0.1:7890,也就是clash的默认端口。虽然我不会go,但ChatGPT会啊,2333

package chatgpt

import (
	"QQ-ChatGPT-Bot/config"
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"net/url"
	"strings"
)

const OpenaiApiUrl = "https://api.openai.com/v1/chat/completions"

type OpenAiRcv struct {
	Id      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	Model   string `json:"model"`
	Choices []struct {
		Message struct {
			Role    string `json:"role"`
			Content string `json:"content"`
		} `json:"message"`
		FinishReason string `json:"finish_reason"`
	} `json:"choices"`
	Usage struct {
		PromptTokens    int `json:"prompt_tokens"`
		CompletionTokes int `json:"completion_tokens"`
		TotalTokens     int `json:"total_tokens"`
	}
}

// GenerateText 调用openai的API生成文本
func GenerateText(text string) string {
	log.Println("正在调用OpenAI API生成文本...", text)
	proxyURL, err := url.Parse("http://127.0.0.1:7890")
	if err != nil {
		log.Println(err)
	}
	httpClient := &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyURL(proxyURL),
		},
	}
	postData := []byte(fmt.Sprintf(`{
	  "model": "%s",
	  "messages": %s,
	  "max_tokens": %d,
	  "temperature": %.1f
	}`, config.Cfg.OpenAi.Model, "[{\"role\": \"user\", \"content\": \""+text+"\"}]", config.Cfg.OpenAi.MaxTokens, config.Cfg.OpenAi.Temperature))
	req, _ := http.NewRequest("POST", OpenaiApiUrl, bytes.NewBuffer(postData))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer "+config.Cfg.OpenAi.ApiKey)
	resp, err := httpClient.Do(req)
	if err != nil {
		log.Println(err)
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	var openAiRcv OpenAiRcv
	err = json.Unmarshal(body, &openAiRcv)
	if err != nil {
		log.Println(err)
	}
	if len(openAiRcv.Choices) == 0 {
		log.Println("OpenAI API调用失败,返回内容:", string(body))
		return string(body)
	}
	openAiRcv.Choices[0].Message.Content = strings.ReplaceAll(openAiRcv.Choices[0].Message.Content, "\n\n", "")
	log.Printf("Model: %s TotalTokens: %d+%d=%d", openAiRcv.Model, openAiRcv.Usage.PromptTokens, openAiRcv.Usage.CompletionTokes, openAiRcv.Usage.TotalTokens)
	return openAiRcv.Choices[0].Message.Content
}

给你点赞👍🏻

@SuInk SuInk closed this as completed Mar 27, 2023
@SuInk SuInk pinned this issue Apr 3, 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

No branches or pull requests

2 participants