如何指定 Open AI 补全应返回的字数?
例如想象我问人工智能这个问题
埃隆·马斯克是谁?
我可以使用什么参数来确保 AI 发回的结果小于或等于 300 个字?
我认为max_tokens参数是为了这个,但它似乎max_tokens是为了分解输入而不是输出。
我想使用 OpenAI 的 TLDR 从 2-3 页的文章中生成 3-6 句话的摘要。我已经粘贴了文章文本,但输出似乎只停留在 1 到 2 句话之间。
有没有办法训练大型语言模型(LLM)来存储特定的上下文?例如,我有一个很长的故事,我想提出问题,但我不想把整个故事放在每个提示中。如何才能让LLM“记住这个故事”?
我的 MERN 堆栈代码文件中有这个,并且运行良好。
exports.chatbot = async (req, res) => {
console.log("OpenAI Chatbot Post");
const { textInput } = req.body;
try {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `
What is your name?
My name is Chatbot.
How old are you?
I am 900 years old.
${textInput}`,
max_tokens: 100,
temperature: 0,
});
if (response.data) {
if (response.data.choices[0].text) {
return res.status(200).json(response.data.choices[0].text);
}
}
} catch (err) {
return res.status(404).json({ message: err.message });
}
};
Run Code Online (Sandbox Code Playgroud)
当我更改API请求时,使用新的API来完成聊天,这个不起作用(API代码来自openAI网站,并且适用于postman)
exports.chatbot = async (req, res) …Run Code Online (Sandbox Code Playgroud) 尝试调用刚刚为 ChatGPT 发布的 got-3.5-turbo API,但我收到了错误的请求错误?
var body = new
{
model = "gpt-3.5-turbo",
messages = data
};
string jsonMessage = JsonConvert.SerializeObject(body);
using (HttpClient client = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpRequestMessage requestMessage = new
HttpRequestMessage(HttpMethod.Post, "https://api.openai.com/v1/completions")
{
Content = new StringContent(jsonMessage, Encoding.UTF8, "application/json")
};
string api_key = PageExtension_CurrentUser.Community.CAIChatGPTAPIKey.Length > 30 ? PageExtension_CurrentUser.Community.CAIChatGPTAPIKey : Genesis.Generic.ReadAppSettingsValue("chatGPTAPIKey");
requestMessage.Headers.Add("Authorization", $"Bearer {api_key}");
HttpResponseMessage response = client.SendAsync(requestMessage).Result;
if (response.StatusCode == HttpStatusCode.OK)
{
string responseData = response.Content.ReadAsStringAsync().Result;
dynamic responseObj = JsonConvert.DeserializeObject(responseData);
string choices = responseObj.choices[0].text;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 ChatGPT 用于我的Telegram机器人。我曾经使用“text-davinci-003”模型,它运行良好(即使现在也运行良好),但我对其响应不满意。
现在我尝试将模型更改为“gpt-3.5-turbo”,它抛出一个 404 响应代码,其中包含文本“错误:请求失败,状态代码 404”,仅此而已。这是我的代码:
import { Configuration, OpenAIApi } from "openai";
import { env } from "../utils/env.js";
const model = "gpt-3.5-turbo"; // works fine when it's "text-davinci-003"
const configuration = new Configuration({
apiKey: env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
export async function getChatGptResponse(request) {
try {
const response = await openai.createCompletion({
model,
prompt: request, // request comes as a string
max_tokens: 2000,
temperature: 1,
stream: false
});
console.log("Full response: ", response, `Choices: `, ...response.data.choices) …Run Code Online (Sandbox Code Playgroud) 这是我的代码片段:
const { Configuration, OpenAI, OpenAIApi } = require ("openai");
const configuration = new Configuration({
apiKey: 'MY KEY'
})
const openai = new OpenAIApi(configuration)
async function start() {
const response = await openai.createChatCompletion({
model:"text-davinci-003",
prompt: "Write a 90 word essay about Family Guy",
temperature: 0,
max_tokens: 1000
})
console.log(response.data.choices[0].text)
}
start()
Run Code Online (Sandbox Code Playgroud)
当我跑步时:node index
我遇到这个问题:
data: {
error: {
message: 'Invalid URL (POST /v1/chat/completions)',
type: 'invalid_request_error',
param: null,
code: null
}
}
},
isAxiosError: true,
toJSON: [Function: toJSON]
}
Run Code Online (Sandbox Code Playgroud)
Node.js …
更新:他们似乎在 API 文档中犯了一个错误,现在已修复。
早些时候,它说“当调用gpt-4-vision-preview或 时gpt-3.5-turbo”,但现在改为“当调用gpt-4-1106-preview或 时gpt-3.5-turbo-1106”。
根据Text Generation - OpenAI API,“当调用gpt-4-vision-preview或时gpt-3.5-turbo,您可以将 response_format 设置{ type: "json_object" }为启用 JSON 模式。”
但是,以下代码会引发错误:
{'error': {'message': '1 validation error for Request\nbody -> response_format\n extra fields not permitted (type=value_error.extra)', 'type': 'invalid_request_error', 'param': None, 'code': None}}
Run Code Online (Sandbox Code Playgroud)
如果我发表评论"response_format": {"type": "json_object"},效果很好。
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"response_format": {"type": "json_object"},
"messages": [
{ …Run Code Online (Sandbox Code Playgroud) 我想使用 GPT 4 模型将 csv 文件中的文本翻译成英语,但我不断收到以下错误。即使我更新了版本,我仍然收到相同的错误。
\nimport openai\nimport pandas as pd\nimport os\nfrom tqdm import tqdm\n\n\nopenai.api_key = os.getenv("API")\n\ndef translate_text(text):\n response = openai.Completion.create(\n model="text-davinci-003", # GPT-4 modeli\n prompt=f"Translate the following Turkish text to English: '{text}'",\n max_tokens=60\n )\n # Yeni API yap\xc4\xb1s\xc4\xb1na g\xc3\xb6re yan\xc4\xb1t\xc4\xb1n al\xc4\xb1nmas\xc4\xb1\n return response.choices[0].text.strip()\n\ndf = pd.read_excel('/content/3500-turkish-dataset-column-name.xlsx')\n\ncolumn_to_translate = 'review'\n\ndf[column_to_translate + '_en'] = ''\n\nfor index, row in tqdm(df.iterrows(), total=df.shape[0]):\n translated_text = translate_text(row[column_to_translate])\n df.at[index, column_to_translate + '_en'] = translated_text\n\ndf.to_csv('path/to/your/translated_csvfile.csv', index=False)\n\nRun Code Online (Sandbox Code Playgroud)\n 0%| | 0/3500 [00:00<?, ?it/s]\n---------------------------------------------------------------------------\nAPIRemovedInV1 Traceback (most recent call last)\n<ipython-input-27-337b5b6f4d32> in …Run Code Online (Sandbox Code Playgroud) 我尝试了以下代码,但只得到了部分结果,例如
[{"light_id": 0, "color
Run Code Online (Sandbox Code Playgroud)
我期待本页建议的完整 JSON:
https://medium.com/@richardhayes777/using-chatgpt-to-control-hue-lights-37729959d94f
import json
import os
import time
from json import JSONDecodeError
from typing import List
import openai
openai.api_key = "xxx"
HEADER = """
I have a hue scale from 0 to 65535.
red is 0.0
orange is 7281
yellow is 14563
purple is 50971
pink is 54612
green is 23665
blue is 43690
Saturation is from 0 to 254
Brightness is from 0 to 254
Two JSONs should be returned in a list. Each …Run Code Online (Sandbox Code Playgroud) openai-api ×10
gpt-3 ×5
chatgpt-api ×3
node.js ×3
python ×3
javascript ×2
axios ×1
gpt-4 ×1
post ×1
telegram-bot ×1