x-dr / chatgptProxyAPI

🔥 使用cloudflare 搭建免费的 OpenAI api代理 ,解决网络无法访问问题。支持流式输出
https://chatai.451024.xyz
MIT License
2.94k stars 679 forks source link

CF部署后怎么使用没大看明白 #12

Closed zwjie12 closed 1 year ago

zwjie12 commented 1 year ago

请问这段说明中的代码是要放到哪里运行?没大看明白。 image

x-dr commented 1 year ago

用法

JavaScript用fetch ```javascript const requestOptions = { method: 'POST', headers: { "Authorization": "Bearer sk-xxxxxxxxxxxx", "Content-Type": "application/json" }, body: JSON.stringify({ "model": "gpt-3.5-turbo", "messages": [ { "role": "user", "content": "hello word" } ] }) }; fetch("https://openai.1rmb.tk/v1/chat/completions", requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error)); ```
用python ```python import requests url = "https://openai.1rmb.tk/v1/chat/completions" api_key = 'sk-xxxxxxxxxxxxxxxxxxxx' headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } payload = { "model": "gpt-3.5-turbo", "messages": [ { "role": "user", "content": "hello word" } ] } try: response = requests.post(url, headers=headers, json=payload) response.raise_for_status() # 抛出异常,如果响应码不是200 data = response.json() print(data) except requests.exceptions.RequestException as e: print(f"请求错误: {e}") except json.JSONDecodeError as e: print(f"无效的 JSON 响应: {e}") ```
用nodejs chatgpt库 [transitive-bullshit/chatgpt-api](https://github.com/transitive-bullshit/chatgpt-api) ```javascript import { ChatGPTAPI } from 'chatgpt' async function example() { const api = new ChatGPTAPI({ apiKey: "sk-xxxxxxxxxxxxxx", // proxy+/v1 apiBaseUrl:"https://openai.1rmb.tk/v1" }) const res = await api.sendMessage('Hello World!') console.log(res.text) } example() ```