-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLM.py
More file actions
65 lines (49 loc) · 1.92 KB
/
LLM.py
File metadata and controls
65 lines (49 loc) · 1.92 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
import requests
import json
import markdown
import os
from dotenv import load_dotenv
import re
load_dotenv()
def send_message(user_input):
url = 'https://openrouter.ai/api/v1/chat/completions'
user_input = """Provide agricultural info on a given species covering:
Types (scientific & common names)
Common diseases
Nutrient needs
Best harvest season
Farming needs & cost
Market price in India
Ideal temperature & climate """+user_input
# API key directly from the uploaded code
API_KEY = os.getenv('LLM_API_KEY')
if not API_KEY:
return "Error: API Key is missing."
headers = {
'Authorization': f'Bearer {API_KEY}',
'HTTP-Referer': 'https://www.sitename.com',
'X-Title': 'SiteName',
'Content-Type': 'application/json',
}
body = {
'model': 'deepseek/deepseek-r1:free',
'messages': [{'role': 'user', 'content': user_input}],
}
try:
# Use the json parameter for automatic JSON encoding
response = requests.post(url, headers=headers, json=body)
response.raise_for_status() # Raises an error for bad responses (4xx, 5xx)
# Extract markdown content from the response
data = response.json()
markdown_text = data.get('choices', [{}])[0].get('message', {}).get('content', 'No response received.')
# return(markdown_text)
# Convert markdown text to HTML
html_response = markdown.markdown(markdown_text)
html_response = re.sub(r'<hr\s*/?>', '<div class="mt-4"></div>', html_response)
return html_response
except requests.exceptions.HTTPError as e:
return f"HTTP error occurred: {e}"
except requests.exceptions.RequestException as e:
return f"Request error occurred: {e}"
except Exception as e:
return f"An unexpected error occurred: {e}"