Skip to content

Commit b6c78d1

Browse files
Fix tutorial (Ollama): add PDF support and correct model config (#193)
- Remove invalid params wrapper from OllamaModel initialization - Add PyPDF2 support to file_read tool for PDF extraction - Update requirements.txt with PyPDF2 dependency - Fix configuration to use direct parameters instead of params dict
1 parent 3582e09 commit b6c78d1

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

01-tutorials/01-fundamentals/02-model-providers/01-ollama-model/ollama_file_ops_agent.ipynb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
},
9898
"outputs": [],
9999
"source": [
100-
"!ollama pull llama3.2:1b"
100+
"!ollama pull llama3.2:3b"
101101
]
102102
},
103103
{
@@ -148,11 +148,9 @@
148148
"outputs": [],
149149
"source": [
150150
"# File Operation Tools\n",
151-
"\n",
152-
"\n",
153151
"@tool\n",
154152
"def file_read(file_path: str) -> str:\n",
155-
" \"\"\"Read a file and return its content.\n",
153+
" \"\"\"Read a file and return its content. Supports both text and PDF files.\n",
156154
"\n",
157155
" Args:\n",
158156
" file_path (str): Path to the file to read\n",
@@ -164,8 +162,19 @@
164162
" FileNotFoundError: If the file doesn't exist\n",
165163
" \"\"\"\n",
166164
" try:\n",
167-
" with open(file_path, \"r\") as file:\n",
168-
" return file.read()\n",
165+
" # Check if it's a PDF file\n",
166+
" if file_path.lower().endswith('.pdf'):\n",
167+
" import PyPDF2\n",
168+
" with open(file_path, \"rb\") as file:\n",
169+
" pdf_reader = PyPDF2.PdfReader(file)\n",
170+
" text = \"\"\n",
171+
" for page in pdf_reader.pages:\n",
172+
" text += page.extract_text() + \"\\n\"\n",
173+
" return text if text.strip() else \"Error: Could not extract text from PDF\"\n",
174+
" else:\n",
175+
" # Regular text file\n",
176+
" with open(file_path, \"r\", encoding=\"utf-8\") as file:\n",
177+
" return file.read()\n",
169178
" except FileNotFoundError:\n",
170179
" return f\"Error: File '{file_path}' not found.\"\n",
171180
" except Exception as e:\n",
@@ -268,7 +277,7 @@
268277
"\"\"\"\n",
269278
"\n",
270279
"model_id = (\n",
271-
" \"llama3.2:1b\" # You can change this to any model you have pulled with Ollama.\n",
280+
" \"llama3.2:3b\" # You can change this to any model you have pulled with Ollama.\n",
272281
")"
273282
]
274283
},
@@ -289,12 +298,9 @@
289298
"ollama_model = OllamaModel(\n",
290299
" model_id=model_id,\n",
291300
" host=\"http://localhost:11434\",\n",
292-
" params={\n",
293-
" \"max_tokens\": 4096, # Adjust based on your model's capabilities\n",
294-
" \"temperature\": 0.7, # Lower for more deterministic responses, higher for more creative\n",
295-
" \"top_p\": 0.9, # Nucleus sampling parameter\n",
296-
" \"stream\": True, # Enable streaming responses\n",
297-
" },\n",
301+
" max_tokens=4096, # Adjust based on your model's capabilities\n",
302+
" temperature=0.7, # Lower for more deterministic responses, higher for more creative\n",
303+
" top_p=0.9, # Nucleus sampling parameter\n",
298304
")\n",
299305
"\n",
300306
"# Create the agent\n",
@@ -378,7 +384,7 @@
378384
],
379385
"metadata": {
380386
"kernelspec": {
381-
"display_name": "Python 3 (ipykernel)",
387+
"display_name": "genai-on-aws",
382388
"language": "python",
383389
"name": "python3"
384390
},
@@ -392,7 +398,7 @@
392398
"name": "python",
393399
"nbconvert_exporter": "python",
394400
"pygments_lexer": "ipython3",
395-
"version": "3.12.9"
401+
"version": "3.12.1"
396402
}
397403
},
398404
"nbformat": 4,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
strands-agents
22
strands-agents-tools
33
strands-agents[ollama]
4+
PyPDF2>=3.0.0

0 commit comments

Comments
 (0)