File size: 992 Bytes
a74a385 | 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 | import requests
# Ollama configuration
OLLAMA_BASE_URL = "http://localhost:11434"
MODEL_NAME = "mistral"
def ask_ollama(prompt, model=MODEL_NAME):
url = f"{OLLAMA_BASE_URL}/api/generate"
headers = {"Content-Type": "application/json"}
payload = {
"model": model,
"prompt": prompt,
"stream": False
}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
return data.get("response", "No response from model.")
except Exception as e:
return f"Error: {e}"
# Chat loop
def chat():
print("🤖 TinyLLaMA Chatbot (type 'exit' to quit)")
while True:
user_input = input("You: ")
if user_input.lower() in {"exit", "quit"}:
print("Goodbye!")
break
response = ask_ollama(user_input)
print(f"Bot: {response}")
if __name__ == "__main__":
chat()
|