OpenAI接口规范调用本地Ollama模型

Python

Python
import openai

client = openai.OpenAI(
    api_key="your_api_key_this_is_not_necessary_for_local_ollama_you_can_write_any_characters_just_like_this",
    base_url="http://127.0.0.1:11434/v1"
)

response = client.chat.completions.create(
     model="deepseek-r1:1.5b",
     messages=[
         {"role": "system", "content": "You are a helpful assistant."},
         {"role": "user", "content": "Hello~"},
     ],
     stream=False
)
 
print(response.choices[0].message.content)