from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url="https://api.lmtchina.com/v1"
)
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
{"role": "user", "content": "使用中文回答,Compose a poem that explains the concept of recursion in programming."}
]
)
print(completion.choices[0].message)
结果:
更新:
以下兩種方法都可
(1)使用client
from openai import OpenAI
import os
import openai
client=OpenAI()
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url="https://api.lmtchina.com/v1"
)
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "say hello world",
},
],
)
print(completion.choices[0].message.content)
(2)不使用client
import openai
import os
openai.api_key =os.environ["OPENAI_API_KEY"]
openai.base_url = "https://api.lmtchina.com/v1/" #最後的/不要省略
completion = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "say hello world",
},
],
)
print(completion.choices[0].message.content)