from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(
openai_api_base="https://api.allmhub.com/v1/", openai_api_key="sk-xxxxx",
)
res = llm.predict("hello")
print(res)import openai
def query_gpt4(question):
openai.api_key = "sk-xxx"
#openai.base_url = url
openai.base_url = 'https://api.allmhub.com/v1/'
try:
response = openai.chat.completions.create(
model="gpt-4", # 确认使用 GPT-4 模型
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": question}
]
)
print(response)
return response['choices'][0].message['content']
except Exception as e:
return str(e)
# 问题
question = "为什么太阳那么红?"
# 获取并打印回答
answer = query_gpt4(question)
print(answer)
# 使用方法
若出现module 'openai' has no attribute 'chat'报错,请更新openai库:pip3 install --upgrade openai
若出现module 'attr' has no attribute 'frozen'报错,请更新输入:pip3 install --upgrade attrsimport requests
import json
def get_chat_gpt_response(prompt):
url = "https://api.allmhub.com/v1/chat/completions"
headers = {
"Authorization": "Bearer apikey",
"Content-Type": "application/json"
}
data = {
"model": "claude-3-haiku-20240307",
"messages": [{"role": "user", "content": prompt}]
}
response = requests.post(url, headers=headers, json=data)
print(json.loads(response.text))
return response
resp = get_chat_gpt_response('给我画一个可爱的女生穿着绿色的卫衣')
print(resp.text)import http.client
import json
conn = http.client.HTTPSConnection("api.allmhub.com")
payload = json.dumps({
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
],
"stream": True
})
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer {{YOUR_API_KEY}}',
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Content-Type': 'application/json'
}
conn.request("POST", "/v1/chat/completions", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))import requests
import json
def analyze_image(image_url, api_url, openai_key):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {openai_key}'
}
data = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What’s in this image?"},
{
"type": "image_url",
"image_url": {
"url": image_url,
},
},
],
}
],
"max_tokens": 4000,
}
response = requests.post(api_url, headers=headers, data=json.dumps(data))
print(response.json())
# 使用方法
analyze_image("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", "https://api.allmhub.com/v1/chat/completions", "sk-xxxxxxx")