-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathollama_langchain_tutorial_marktechpost.py
More file actions
618 lines (502 loc) Β· 19.5 KB
/
ollama_langchain_tutorial_marktechpost.py
File metadata and controls
618 lines (502 loc) Β· 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# -*- coding: utf-8 -*-
"""ollama_langchain_tutorial_Marktechpost.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1NcQL_jwAowtjqvXcjTrRg-M_Bx9iJd4R
"""
import os
import sys
import subprocess
import time
import threading
import queue
import json
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass
from contextlib import contextmanager
import asyncio
from concurrent.futures import ThreadPoolExecutor
def install_packages():
"""Install required packages for Colab environment"""
packages = [
"langchain",
"langchain-community",
"langchain-core",
"chromadb",
"sentence-transformers",
"faiss-cpu",
"pypdf",
"python-docx",
"requests",
"psutil",
"pyngrok",
"gradio"
]
for package in packages:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
install_packages()
import requests
import psutil
import threading
from queue import Queue
from langchain.llms.base import LLM
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.schema import BaseMessage, HumanMessage, AIMessage, SystemMessage
from langchain.memory import ConversationBufferWindowMemory, ConversationSummaryBufferMemory
from langchain.chains import ConversationChain, RetrievalQA
from langchain.prompts import PromptTemplate, ChatPromptTemplate
from langchain.document_loaders import PyPDFLoader, TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS, Chroma
from langchain.agents import AgentType, initialize_agent, Tool
from langchain.tools import DuckDuckGoSearchRun
@dataclass
class OllamaConfig:
"""Configuration for Ollama setup"""
model_name: str = "llama2"
base_url: str = "http://localhost:11434"
max_tokens: int = 2048
temperature: float = 0.7
gpu_layers: int = -1
context_window: int = 4096
batch_size: int = 512
threads: int = 4
class OllamaManager:
"""Advanced Ollama manager for Colab environment"""
def __init__(self, config: OllamaConfig):
self.config = config
self.process = None
self.is_running = False
self.models_cache = {}
self.performance_monitor = PerformanceMonitor()
def install_ollama(self):
"""Install Ollama in Colab environment"""
try:
subprocess.run([
"curl", "-fsSL", "https://ollama.com/install.sh", "-o", "/tmp/install.sh"
], check=True)
subprocess.run(["bash", "/tmp/install.sh"], check=True)
print("β
Ollama installed successfully")
except subprocess.CalledProcessError as e:
print(f"β Failed to install Ollama: {e}")
raise
def start_server(self):
"""Start Ollama server with GPU support"""
if self.is_running:
print("Ollama server is already running")
return
try:
env = os.environ.copy()
env["OLLAMA_NUM_PARALLEL"] = str(self.config.threads)
env["OLLAMA_MAX_LOADED_MODELS"] = "3"
self.process = subprocess.Popen(
["ollama", "serve"],
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
time.sleep(5)
if self.health_check():
self.is_running = True
print("β
Ollama server started successfully")
self.performance_monitor.start()
else:
raise Exception("Server failed to start properly")
except Exception as e:
print(f"β Failed to start Ollama server: {e}")
raise
def health_check(self) -> bool:
"""Check if Ollama server is healthy"""
try:
response = requests.get(f"{self.config.base_url}/api/tags", timeout=10)
return response.status_code == 200
except:
return False
def pull_model(self, model_name: str) -> bool:
"""Pull a model from Ollama registry"""
try:
print(f"π Pulling model: {model_name}")
result = subprocess.run(
["ollama", "pull", model_name],
capture_output=True,
text=True,
timeout=1800
)
if result.returncode == 0:
print(f"β
Model {model_name} pulled successfully")
self.models_cache[model_name] = True
return True
else:
print(f"β Failed to pull model {model_name}: {result.stderr}")
return False
except subprocess.TimeoutExpired:
print(f"β Timeout pulling model {model_name}")
return False
except Exception as e:
print(f"β Error pulling model {model_name}: {e}")
return False
def list_models(self) -> List[str]:
"""List available local models"""
try:
result = subprocess.run(
["ollama", "list"],
capture_output=True,
text=True
)
models = []
for line in result.stdout.split('\n')[1:]:
if line.strip():
model_name = line.split()[0]
models.append(model_name)
return models
except Exception as e:
print(f"β Error listing models: {e}")
return []
def stop_server(self):
"""Stop Ollama server"""
if self.process:
self.process.terminate()
self.process.wait()
self.is_running = False
self.performance_monitor.stop()
print("β
Ollama server stopped")
class PerformanceMonitor:
"""Monitor system performance and resource usage"""
def __init__(self):
self.monitoring = False
self.stats = {
"cpu_usage": [],
"memory_usage": [],
"gpu_usage": [],
"inference_times": []
}
self.monitor_thread = None
def start(self):
"""Start performance monitoring"""
self.monitoring = True
self.monitor_thread = threading.Thread(target=self._monitor_loop)
self.monitor_thread.daemon = True
self.monitor_thread.start()
def stop(self):
"""Stop performance monitoring"""
self.monitoring = False
if self.monitor_thread:
self.monitor_thread.join()
def _monitor_loop(self):
"""Main monitoring loop"""
while self.monitoring:
try:
cpu_percent = psutil.cpu_percent(interval=1)
memory = psutil.virtual_memory()
self.stats["cpu_usage"].append(cpu_percent)
self.stats["memory_usage"].append(memory.percent)
for key in ["cpu_usage", "memory_usage"]:
if len(self.stats[key]) > 100:
self.stats[key] = self.stats[key][-100:]
time.sleep(5)
except Exception as e:
print(f"Monitoring error: {e}")
def get_stats(self) -> Dict[str, Any]:
"""Get current performance statistics"""
return {
"avg_cpu": sum(self.stats["cpu_usage"][-10:]) / max(len(self.stats["cpu_usage"][-10:]), 1),
"avg_memory": sum(self.stats["memory_usage"][-10:]) / max(len(self.stats["memory_usage"][-10:]), 1),
"total_inferences": len(self.stats["inference_times"]),
"avg_inference_time": sum(self.stats["inference_times"]) / max(len(self.stats["inference_times"]), 1)
}
class OllamaLLM(LLM):
"""Custom LangChain LLM for Ollama"""
model_name: str = "llama2"
base_url: str = "http://localhost:11434"
temperature: float = 0.7
max_tokens: int = 2048
performance_monitor: Optional[PerformanceMonitor] = None
@property
def _llm_type(self) -> str:
return "ollama"
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Make API call to Ollama"""
start_time = time.time()
try:
payload = {
"model": self.model_name,
"prompt": prompt,
"stream": False,
"options": {
"temperature": self.temperature,
"num_predict": self.max_tokens,
"stop": stop or []
}
}
response = requests.post(
f"{self.base_url}/api/generate",
json=payload,
timeout=120
)
response.raise_for_status()
result = response.json()
inference_time = time.time() - start_time
if self.performance_monitor:
self.performance_monitor.stats["inference_times"].append(inference_time)
return result.get("response", "")
except Exception as e:
print(f"β Ollama API error: {e}")
return f"Error: {str(e)}"
class RAGSystem:
"""Retrieval-Augmented Generation system"""
def __init__(self, llm: OllamaLLM, embedding_model: str = "sentence-transformers/all-MiniLM-L6-v2"):
self.llm = llm
self.embeddings = HuggingFaceEmbeddings(model_name=embedding_model)
self.vector_store = None
self.qa_chain = None
self.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
def add_documents(self, file_paths: List[str]):
"""Add documents to the vector store"""
documents = []
for file_path in file_paths:
try:
if file_path.endswith('.pdf'):
loader = PyPDFLoader(file_path)
else:
loader = TextLoader(file_path)
docs = loader.load()
documents.extend(docs)
except Exception as e:
print(f"β Error loading {file_path}: {e}")
if documents:
splits = self.text_splitter.split_documents(documents)
if self.vector_store is None:
self.vector_store = FAISS.from_documents(splits, self.embeddings)
else:
self.vector_store.add_documents(splits)
self.qa_chain = RetrievalQA.from_chain_type(
llm=self.llm,
chain_type="stuff",
retriever=self.vector_store.as_retriever(search_kwargs={"k": 3}),
return_source_documents=True
)
print(f"β
Added {len(splits)} document chunks to vector store")
def query(self, question: str) -> Dict[str, Any]:
"""Query the RAG system"""
if not self.qa_chain:
return {"answer": "No documents loaded. Please add documents first."}
try:
result = self.qa_chain({"query": question})
return {
"answer": result["result"],
"sources": [doc.metadata for doc in result.get("source_documents", [])]
}
except Exception as e:
return {"answer": f"Error: {str(e)}"}
class ConversationManager:
"""Manage conversation history and memory"""
def __init__(self, llm: OllamaLLM, memory_type: str = "buffer"):
self.llm = llm
self.conversations = {}
self.memory_type = memory_type
def get_conversation(self, session_id: str) -> ConversationChain:
"""Get or create conversation for session"""
if session_id not in self.conversations:
if self.memory_type == "buffer":
memory = ConversationBufferWindowMemory(k=10)
elif self.memory_type == "summary":
memory = ConversationSummaryBufferMemory(
llm=self.llm,
max_token_limit=1000
)
else:
memory = ConversationBufferWindowMemory(k=10)
self.conversations[session_id] = ConversationChain(
llm=self.llm,
memory=memory,
verbose=True
)
return self.conversations[session_id]
def chat(self, session_id: str, message: str) -> str:
"""Chat with specific session"""
conversation = self.get_conversation(session_id)
return conversation.predict(input=message)
def clear_session(self, session_id: str):
"""Clear conversation history for session"""
if session_id in self.conversations:
del self.conversations[session_id]
class OllamaLangChainSystem:
"""Main system integrating all components"""
def __init__(self, config: OllamaConfig):
self.config = config
self.manager = OllamaManager(config)
self.llm = None
self.rag_system = None
self.conversation_manager = None
self.tools = []
self.agent = None
def setup(self):
"""Complete system setup"""
print("π Setting up Ollama + LangChain system...")
self.manager.install_ollama()
self.manager.start_server()
if not self.manager.pull_model(self.config.model_name):
print("β Failed to pull default model")
return False
self.llm = OllamaLLM(
model_name=self.config.model_name,
base_url=self.config.base_url,
temperature=self.config.temperature,
max_tokens=self.config.max_tokens,
performance_monitor=self.manager.performance_monitor
)
self.rag_system = RAGSystem(self.llm)
self.conversation_manager = ConversationManager(self.llm)
self._setup_tools()
print("β
System setup complete!")
return True
def _setup_tools(self):
"""Setup tools for the agent"""
search = DuckDuckGoSearchRun()
self.tools = [
Tool(
name="Search",
func=search.run,
description="Search the internet for current information"
),
Tool(
name="RAG_Query",
func=lambda q: self.rag_system.query(q)["answer"],
description="Query loaded documents using RAG"
)
]
self.agent = initialize_agent(
tools=self.tools,
llm=self.llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
def chat(self, message: str, session_id: str = "default") -> str:
"""Simple chat interface"""
return self.conversation_manager.chat(session_id, message)
def rag_chat(self, question: str) -> Dict[str, Any]:
"""RAG-based chat"""
return self.rag_system.query(question)
def agent_chat(self, message: str) -> str:
"""Agent-based chat with tools"""
return self.agent.run(message)
def switch_model(self, model_name: str) -> bool:
"""Switch to different model"""
if self.manager.pull_model(model_name):
self.llm.model_name = model_name
print(f"β
Switched to model: {model_name}")
return True
return False
def load_documents(self, file_paths: List[str]):
"""Load documents into RAG system"""
self.rag_system.add_documents(file_paths)
def get_performance_stats(self) -> Dict[str, Any]:
"""Get system performance statistics"""
return self.manager.performance_monitor.get_stats()
def cleanup(self):
"""Clean up resources"""
self.manager.stop_server()
print("β
System cleanup complete")
def main():
"""Main function demonstrating the system"""
config = OllamaConfig(
model_name="llama2",
temperature=0.7,
max_tokens=2048
)
system = OllamaLangChainSystem(config)
try:
if not system.setup():
return
print("\nπ£οΈ Testing basic chat:")
response = system.chat("Hello! How are you?")
print(f"Response: {response}")
print("\nπ Testing model switching:")
models = system.manager.list_models()
print(f"Available models: {models}")
print("\nπ€ Testing agent:")
agent_response = system.agent_chat("What's the current weather like?")
print(f"Agent Response: {agent_response}")
print("\nπ Performance Statistics:")
stats = system.get_performance_stats()
print(json.dumps(stats, indent=2))
except KeyboardInterrupt:
print("\nβΉοΈ Interrupted by user")
except Exception as e:
print(f"β Error: {e}")
finally:
system.cleanup()
def create_gradio_interface(system: OllamaLangChainSystem):
"""Create a Gradio interface for easy interaction"""
try:
import gradio as gr
def chat_interface(message, history, mode):
if mode == "Basic Chat":
response = system.chat(message)
elif mode == "RAG Chat":
result = system.rag_chat(message)
response = result["answer"]
elif mode == "Agent Chat":
response = system.agent_chat(message)
else:
response = "Unknown mode"
history.append((message, response))
return "", history
def upload_docs(files):
if files:
file_paths = [f.name for f in files]
system.load_documents(file_paths)
return f"Loaded {len(file_paths)} documents into RAG system"
return "No files uploaded"
def get_stats():
stats = system.get_performance_stats()
return json.dumps(stats, indent=2)
with gr.Blocks(title="Ollama + LangChain System") as demo:
gr.Markdown("# π¦ Ollama + LangChain Advanced System")
with gr.Tab("Chat"):
chatbot = gr.Chatbot()
mode = gr.Dropdown(
["Basic Chat", "RAG Chat", "Agent Chat"],
value="Basic Chat",
label="Chat Mode"
)
msg = gr.Textbox(label="Message")
clear = gr.Button("Clear")
msg.submit(chat_interface, [msg, chatbot, mode], [msg, chatbot])
clear.click(lambda: ([], ""), outputs=[chatbot, msg])
with gr.Tab("Document Upload"):
file_upload = gr.File(file_count="multiple", label="Upload Documents")
upload_btn = gr.Button("Upload to RAG System")
upload_status = gr.Textbox(label="Status")
upload_btn.click(upload_docs, file_upload, upload_status)
with gr.Tab("Performance"):
stats_btn = gr.Button("Get Performance Stats")
stats_output = gr.Textbox(label="Performance Statistics")
stats_btn.click(get_stats, outputs=stats_output)
return demo
except ImportError:
print("Gradio not installed. Skipping interface creation.")
return None
if __name__ == "__main__":
print("π Ollama + LangChain System for Google Colab")
print("=" * 50)
main()
# Or create a system instance for interactive use
# config = OllamaConfig(model_name="llama2")
# system = OllamaLangChainSystem(config)
# system.setup()
# # Create Gradio interface
# demo = create_gradio_interface(system)
# if demo:
# demo.launch(share=True) # share=True for public link