Spaces:
Running
Running
Update helper_functions_api.py
Browse files- helper_functions_api.py +50 -0
helper_functions_api.py
CHANGED
@@ -12,6 +12,43 @@ def md_to_html(md_text):
|
|
12 |
html_content = markdown.markdown(md_text,extensions=["extra"])
|
13 |
return html_content.replace('\n', '')
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
####------------------------------ OPTIONAL--> User id and persistant data storage-------------------------------------####
|
16 |
from datetime import datetime
|
17 |
import psycopg2
|
@@ -27,6 +64,7 @@ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
27 |
HELICON_API_KEY = os.getenv("HELICON_API_KEY")
|
28 |
SUPABASE_USER = os.environ['SUPABASE_USER']
|
29 |
SUPABASE_PASSWORD = os.environ['SUPABASE_PASSWORD']
|
|
|
30 |
|
31 |
def insert_data(user_id, user_query, subtopic_query, response, html_report):
|
32 |
# Connect to your database
|
@@ -118,6 +156,10 @@ groq_client = OpenAI(
|
|
118 |
base_url="https://groq.hconeai.com/openai/v1",
|
119 |
default_headers={ "Helicone-Auth": f"Bearer {HELICON_API_KEY}"})
|
120 |
|
|
|
|
|
|
|
|
|
121 |
# Groq model names
|
122 |
llm_default_small = "llama3-8b-8192"
|
123 |
llm_default_medium = "llama3-70b-8192"
|
@@ -148,6 +190,14 @@ def together_response(message, model = llm_default_small, SysPrompt = SysPromptD
|
|
148 |
response = together_client.chat.completions.create(**params)
|
149 |
return response.choices[0].message.content
|
150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
def json_from_text(text):
|
152 |
"""
|
153 |
Extracts JSON from text using regex and fuzzy JSON loading.
|
|
|
12 |
html_content = markdown.markdown(md_text,extensions=["extra"])
|
13 |
return html_content.replace('\n', '')
|
14 |
|
15 |
+
def has_tables(html_string):
|
16 |
+
try:
|
17 |
+
# Use BeautifulSoup with lxml parser
|
18 |
+
soup = BeautifulSoup(html_string, 'lxml')
|
19 |
+
|
20 |
+
# First, try BeautifulSoup's find_all method
|
21 |
+
if soup.find_all('table'):
|
22 |
+
return True
|
23 |
+
|
24 |
+
# If no tables found, try a more aggressive search using lxml's XPath
|
25 |
+
tree = etree.HTML(str(soup))
|
26 |
+
return len(tree.xpath('//table')) > 0
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
# Log the exception if needed
|
30 |
+
print(f"An error occurred: {str(e)}")
|
31 |
+
return False
|
32 |
+
|
33 |
+
def extract_data_from_tag(input_string, tag):
|
34 |
+
# Create the regex pattern
|
35 |
+
pattern = f'<{tag}.*?>(.*?)</{tag}>'
|
36 |
+
|
37 |
+
# Find all matches
|
38 |
+
matches = re.findall(pattern, input_string, re.DOTALL)
|
39 |
+
|
40 |
+
# If matches are found, return them joined by newlines
|
41 |
+
if matches:
|
42 |
+
out = '\n'.join(match.strip() for match in matches)
|
43 |
+
# Check for incorrect tagging
|
44 |
+
if len(out) > 0.8*len(input_string):
|
45 |
+
return out
|
46 |
+
else:
|
47 |
+
return input_string
|
48 |
+
|
49 |
+
# If no matches are found, return the original string
|
50 |
+
return input_string
|
51 |
+
|
52 |
####------------------------------ OPTIONAL--> User id and persistant data storage-------------------------------------####
|
53 |
from datetime import datetime
|
54 |
import psycopg2
|
|
|
64 |
HELICON_API_KEY = os.getenv("HELICON_API_KEY")
|
65 |
SUPABASE_USER = os.environ['SUPABASE_USER']
|
66 |
SUPABASE_PASSWORD = os.environ['SUPABASE_PASSWORD']
|
67 |
+
OPENROUTER_API_KEY = os.environ['OPENROUTER_API_KEY']
|
68 |
|
69 |
def insert_data(user_id, user_query, subtopic_query, response, html_report):
|
70 |
# Connect to your database
|
|
|
156 |
base_url="https://groq.hconeai.com/openai/v1",
|
157 |
default_headers={ "Helicone-Auth": f"Bearer {HELICON_API_KEY}"})
|
158 |
|
159 |
+
or_client = OpenAI(
|
160 |
+
base_url="https://openrouter.ai/api/v1",
|
161 |
+
api_key=OPENROUTER_API_KEY)
|
162 |
+
|
163 |
# Groq model names
|
164 |
llm_default_small = "llama3-8b-8192"
|
165 |
llm_default_medium = "llama3-70b-8192"
|
|
|
190 |
response = together_client.chat.completions.create(**params)
|
191 |
return response.choices[0].message.content
|
192 |
|
193 |
+
def openrouter_response(messages,model="meta-llama/llama-3-70b-instruct:nitro"):
|
194 |
+
response = or_client.chat.completions.create(
|
195 |
+
model=model,
|
196 |
+
messages=messages,
|
197 |
+
max_tokens=4096,
|
198 |
+
)
|
199 |
+
return response.choices[0].message.content
|
200 |
+
|
201 |
def json_from_text(text):
|
202 |
"""
|
203 |
Extracts JSON from text using regex and fuzzy JSON loading.
|