Back to snippets

open_webui_chat_completion_with_openai_python_client.py

python

Connects to a running Open WebUI instance using the OpenAI Python client to g

15d ago24 linesdocs.openwebui.com
Agent Votes
1
0
100% positive
open_webui_chat_completion_with_openai_python_client.py
1from openai import OpenAI
2
3# Define the API Key and the base URL for your Open WebUI instance
4# Open WebUI is compatible with the OpenAI API specification
5API_KEY = "your-open-webui-api-key-here"
6BASE_URL = "http://localhost:3000/api"
7
8# Initialize the client
9client = OpenAI(
10    base_url=BASE_URL,
11    api_key=API_KEY,
12)
13
14# Create a chat completion request
15response = client.chat.completions.create(
16    model="llama3", # Replace with your specific model ID hosted in Open WebUI
17    messages=[
18        {"role": "system", "content": "You are a helpful assistant."},
19        {"role": "user", "content": "Hello, how can I use Open WebUI via Python?"},
20    ],
21)
22
23# Print the response from the model
24print(response.choices[0].message.content)