⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ollama-python-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# How to Integrate Local LLMs With Ollama and Python

This folder provides the code examples for the Real Python tutorial [How to Integrate Local LLMs With Ollama and Python](https://realpython.com/ollama-python/).
11 changes: 11 additions & 0 deletions ollama-python-sdk/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ollama import chat

messages = [
{
"role": "user",
"content": "Explain what Python is in one sentence.",
},
]

response = chat(model="llama3.2:latest", messages=messages)
print(response.message.content)
24 changes: 24 additions & 0 deletions ollama-python-sdk/chat_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from ollama import chat

messages = [
{
"role": "system",
"content": "You are an expert Python tutor.",
},
{
"role": "user",
"content": "Define list comprehensions in a sentence.",
},
]
response = chat(model="llama3.2:latest", messages=messages)
print(response.message.content)

messages.append(response.message) # Keep context
messages.append(
{
"role": "user",
"content": "Provide a short, practical example.",
}
)
response = chat(model="llama3.2:latest", messages=messages)
print(response.message.content)
Empty file.
8 changes: 8 additions & 0 deletions ollama-python-sdk/generate_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from ollama import generate

response = generate(
model="llama3.2:latest",
prompt="Explain what Python is in one sentence.",
)

print(response.response)
15 changes: 15 additions & 0 deletions ollama-python-sdk/streams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from ollama import chat

stream = chat(
model="llama3.2:latest",
messages=[
{
"role": "user",
"content": "Explain Python dataclasses with a quick example.",
}
],
stream=True,
)

for chunk in stream:
print(chunk.message.content, end="", flush=True)
51 changes: 51 additions & 0 deletions ollama-python-sdk/tool_calling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import math

from ollama import chat


# Define a tool as a Python function
def square_root(number: float) -> float:
"""Calculate the square root of a number.

Args:
number: The number to calculate the square root for.

Returns:
The square root of the number.
"""
return math.sqrt(number)


messages = [
{
"role": "user",
"content": "What is the square root of 36?",
}
]

response = chat(
model="llama3.2:latest",
messages=messages,
tools=[square_root], # Pass the tools along with the prompt
)

# Append the response for context
messages.append(response.message)

if response.message.tool_calls:
tool = response.message.tool_calls[0]
# Call the tool
result = square_root(float(tool.function.arguments["number"]))

# Append the tool result
messages.append(
{
"role": "tool",
"tool_name": tool.function.name,
"content": str(result),
}
)

# Obtain the final answer
final_response = chat(model="llama3.2:latest", messages=messages)
print(final_response.message.content)