我正在开发一个集成了OpenAI的Web应用程序。我已经实现了标准的聊天提示和响应功能,但我在访问视觉API时遇到了问题。我能找到的所有示例都是用Python编写的。我快速创建了一个Jupyter Notebook,并使用我的API密钥调用了视觉模型,结果非常好。正在寻找将此代码转换为C#的方法,或者在我的应用程序中使用此代码的可行解决方案。
from openai import OpenAIclient = OpenAI(api_key="___")response = client.chat.completions.create( model="gpt-4-vision-preview", messages=[ { "role": "user", "content": [ {"type": "text", "text": "Does this image show a Fender Stratocaster Electric Guitar? Respond with yes or no."}, { "type": "image_url", "image_url": { "url": "https://toneshapers.com/cdn/shop/files/Les-Paul-Standard-Front.jpg", }, }, ], } ], max_tokens=300,)print(response.choices[0])
我尝试从我的OpenAI实例中访问“chat.completions”,但它们似乎不存在。
回答:
文档中包含了一个cURL示例,可以轻松地转换为C#的HttpRequest:
curl https://api.openai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-4-vision-preview", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "What’s in this image?" }, { "type": "image_url", "image_url": { "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" } } ] } ], "max_tokens": 300 }'