Spaces:
Sleeping
Sleeping
File size: 1,716 Bytes
704b133 4722db8 704b133 4722db8 704b133 4722db8 704b133 4722db8 704b133 |
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 |
import requests
import json
from typing import Dict
class OllamaLLM:
def __init__(self, base_url: str, model: str):
self.base_url = base_url.rstrip('/')
self.model = model
def generate(self, question: str, context: str) -> str:
"""Generate answer using RAG context"""
prompt = f"""You are a helpful AI assistant. Use the following context to answer the question accurately and concisely.
Context:
{context}
Question: {question}
Instructions:
- Answer based ONLY on the provided context
- If the answer is not in the context, say "I don't have enough information to answer that"
- Keep your answer clear and concise (max 3-5 sentences)
- Cite specific parts of the context when relevant
Answer:"""
try:
response = requests.post(
f"{self.base_url}/api/generate",
json={
"model": self.model,
"prompt": prompt,
"stream": False,
"options": {
"temperature": 0.2,
"top_p": 0.9,
"top_k": 40
}
},
timeout=60
)
if response.status_code == 200:
result = response.json()
return result.get("response", "Error generating response")
else:
return f"Error: Ollama returned status {response.status_code}"
except requests.exceptions.RequestException as e:
return f"Error connecting to Ollama: {str(e)}"
except Exception as e:
return f"Unexpected error: {str(e)}"
|