我正在使用 OpenAI 来了解有关 API 集成的更多信息,但在运行 Python 程序时我不断运行此代码。我向 ChatGPT 询问了该Invalid URL (POST /v1/engines/gpt-3.5-turbo/chat/completions)错误,但它似乎没有给我正确的解决方案。
注意:我确实安装了最新的 OpenAI 软件包(即0.27.4)。
代码:
import os
import openai
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxx"
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a joke."}
]
response = openai.ChatCompletion.create(
engine="gpt-3.5-turbo",
messages=messages,
max_tokens=50,
n=1,
stop=None,
temperature=0.7,
)
joke = response.choices[0].text.strip()
print(joke)
Run Code Online (Sandbox Code Playgroud) 你好,我正在尝试使用 openai 的 api 构建一个聊天机器人。它的基本功能是读取 pdf、txtfile 等并根据它进行回答。我正在关注https://beebom.com/how-train-ai-chatbot-custom-knowledge-base-chatgpt-api/上的教程
我使用了以下代码并安装了必要的依赖项:
from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os
os.environ["OPENAI_API_KEY"] = 'yourapikey'
def construct_index(directory_path):
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 20
chunk_size_limit = 600
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
index.save_to_disk('index.json')
return index
def chatbot(input_text):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
response = index.query(input_text, response_mode="compact")
return …Run Code Online (Sandbox Code Playgroud) 注意:如果您投反对票,至少请分享原因。我花了很多精力来写这个问题,首先分享了我的代码并做了我自己的研究,所以不确定我还可以添加什么。
我已经使用Scrapy成功抓取网站。我使用 CSS 选择器从网页中提取特定数据。然而,设置起来非常耗时并且容易出错。我希望能够将原始 HTML 传递给 chatGPT 并提出如下问题
“以 JSON 对象格式提供该对象的价格、照片数组、描述、主要功能、街道地址和邮政编码”
下面是所需的输出。为了便于阅读,我截断了描述、主要功能和照片。
{
"price":"$945,000",
"photos":"https://media-cloud.corcoranlabs.com/filters:format(webp)/fit-in/1500x1500/ListingFullAPI/NewTaxi/7625191/mediarouting.vestahub.com/Media/134542874?w=3840&q=75;https://media-cloud.corcoranlabs.com/filters:format(webp)/fit-in/1500x1500/ListingFullAPI/NewTaxi/7625191/mediarouting.vestahub.com/Media/134542875?w=3840&q=75;https://media-cloud.corcoranlabs.com/filters:format(webp)/fit-in/1500x1500/ListingFullAPI/NewTaxi/7625191/mediarouting.vestahub.com/Media/134542876?w=3840&q=75",
"description":"<div>This spacious 2 bedroom 1 bath home easily converts to 3 bedrooms. Featuring a BRIGHT and quiet southern exposure, the expansive great room (with 9ft ceilings) is what sets (...)",
"key features":"Center island;Central air;Dining in living room;Dishwasher",
"street address":"170 West 89th Street, 2D",
"zipcode":"NY 10024",
}
Run Code Online (Sandbox Code Playgroud)
现在我遇到的最大聊天长度为 4096 个字符。所以我决定分块发送页面。然而,即使有一个简单的问题,例如“这个物品的价格是多少?” 我预计答案是“945,000 美元”,但我只收到一大堆文字。我想知道我做错了什么。我听说 AutoGPT 提供了新的灵活性,所以我也想知道这是否可以作为一个解决方案。
我的代码:
import requests
from bs4 import BeautifulSoup, Comment
import openai
import json …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用法学硕士,特别是 OpenAI 和其他 OSS 模型。有很多关于使用 LlamaIndex 创建所有文档的存储然后查询它们的指南。我用一些示例文档进行了尝试,但发现每个查询很快就会变得非常昂贵。我想我使用了 50 页的 PDF 文档,摘要查询每次查询花费了大约 1.5 美元。我看到有很多令牌被发送,所以我假设它为每个查询发送整个文档。考虑到有人可能想要使用数以千万计的记录,我看不出像 LlamaIndex 这样的东西如何能够以经济高效的方式真正发挥作用。
另一方面,我看到 OpenAI 允许你训练 ChatGPT 模型。或者使用其他经过定制培训的法学硕士来查询您自己的数据不是更便宜、更有效吗?我为什么要设置 LlamaIndex?
我的代码如下,它与模型完美配合gpt-3.5-turbo,但不适用于gpt-4:
$your_prompt = "Prompt...."
// GPT-4 API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer [API-KEY]';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = json_encode(array('model' => 'gpt-3.5-turbo',
'messages' => array(
array('role' => 'system', 'content' => 'Your system message here'),
array('role' => 'user', 'content' => $your_prompt))));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
echo $result."<br>";
if (curl_errno($ch)) {
echo 'Curl Error:' . curl_error($ch) . "\n<br>"; …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Windows CMD 中使用curl 通过请求测试 GPT-3 API:
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer MY_KEY" -d "{\"text\": \"is this working\"}" https://api.openai.com/v1/conversations/text-davinci-003/messages
Run Code Online (Sandbox Code Playgroud)
鉴于我确实更改了我的密钥“MY_KEY”。
但我得到了:
{
"error": {
"message": "Invalid URL (POST /v1/conversations/text-davinci-003/messages)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试将模型名称命名为text-davinci-002和text-davinci-001,但得到相同的无效 URL 错误。这里正确的网址是什么?我在文档(或 chatGPT 本身)中找不到它。
我正在尝试使用 Gradio 块作为输入来创建变量。然后,这些变量被发送到一个函数,用于格式化字符串。
\n后面的函数不接受 Gradio 块创建的变量。详细信息如下。
\n这是 Gradio“前端”Gradio 代码,旨在生成我想要的变量:
\nwith gr.Blocks() as main:\n with gr.Tab("The Workout Plan"):\n with gr.Column():\n with gr.Row():\n age = gr.Number(label="Age"), #1 Age\n weight = gr.Number(label="Weight (lbs)"), #2 weight \n sex = gr.Dropdown( #3 sex\n label="Biological Sex",\n choices=["Male", "Female"]),\n with gr.Column():\n goal = gr.Dropdown( #4 goal\n label="What is your primary goal?",\n choices=["Hypertrophy (muscle growth)", "Power Lifting (strength increase)", "Flexibility and Mobility"]),\n location = gr.Dropdown( #5 location\n …Run Code Online (Sandbox Code Playgroud) openai-api ×7
chatgpt-api ×3
python ×3
gpt-3 ×2
langchain ×2
autogpt ×1
block ×1
curl ×1
gpt-4 ×1
gradio ×1
llama-index ×1
php-curl ×1
python-3.x ×1
window ×1