Skip to content

Instantly share code, notes, and snippets.

@cyyeh
Created May 21, 2024 00:03
Show Gist options
  • Save cyyeh/b1042006b4ca067f2a75abd97e3749fb to your computer and use it in GitHub Desktop.
Save cyyeh/b1042006b4ca067f2a75abd97e3749fb to your computer and use it in GitHub Desktop.
Test Ollama using OpenAI compatible API
from openai import OpenAI
client = OpenAI(
api_key='ollama',
base_url="http://localhost:11434/v1",
)
# this will fail, showing openai.NotFoundError: 404 page not found
data = client.embeddings.create(
input='hi',
model='nomic-embed-text',
dimensions=768
)
print(data)
# this will succeed
data = client.chat.completions.create(
model='llama3:8b',
messages=[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What is the capital of the United States?"
}
]
)
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment