Skip to content

Instantly share code, notes, and snippets.

@mahenzon
Created October 4, 2025 20:05
Show Gist options
  • Save mahenzon/bdf4bd0ef872821a4820626da26242cc to your computer and use it in GitHub Desktop.
Save mahenzon/bdf4bd0ef872821a4820626da26242cc to your computer and use it in GitHub Desktop.
Ollama example OpenAI API call
import requests
API_KEY = "EMPTY"
# BASE_URL = "http://localhost:12345/engines/llama.cpp/v1"
BASE_URL = "http://localhost:11223/v1"
URL = f"{BASE_URL}/chat/completions"
# MODEL = "ai/gemma3"
# MODEL = "ai/gemma3n"
MODEL = "gemma3:1b"
headers = {
"Authorization": f"Bearer {API_KEY}",
}
def main():
system_message = {
"role": "system",
"content": (
"You are a helpful assistant. "
"Answer questions clearly an concisely. "
"If you don't know the answer, you have to say so. "
"Otherwise provide concise answer, don't add any extra info that user didn't ask for."
),
}
user_message = {
"role": "user",
# "content": "Hello, who are you?",
# "content": "What is the capital of Russia?",
"content": "What is the capital of Russia? Also give one the most important fact about the capital.",
# "content": (
# "Context: square is red, triangle is yellow, hexagon is blue, circle is green\n"
# "Question: what color is hex?"
# # "Question: what color is circle?"
# ),
}
data = {
"model": MODEL,
"messages": [
system_message,
user_message,
],
}
response = requests.post(URL, json=data, headers=headers)
if response.ok:
# pprint(response.json())
print(response.json()["choices"][0]["message"]["content"])
else:
print(response)
print(response.text)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment