Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,23 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import openai
|
| 4 |
import json
|
| 5 |
from dataclasses import dataclass, field
|
| 6 |
from typing import List
|
| 7 |
-
from
|
| 8 |
from json.decoder import JSONDecodeError
|
| 9 |
from pydantic_settings import BaseSettings
|
|
|
|
| 10 |
|
| 11 |
# Configuration class for API keys and settings
|
| 12 |
class Config(BaseSettings):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
LLM_REGULAR: str = "Meta-Llama-3.3-70B-Instruct"
|
| 17 |
TAVILY_API_KEY: str
|
| 18 |
|
| 19 |
# Load configuration from environment variables
|
| 20 |
config = Config(
|
| 21 |
-
|
| 22 |
TAVILY_API_KEY=os.environ["TAVILY_API_KEY"]
|
| 23 |
)
|
| 24 |
|
|
@@ -47,19 +46,24 @@ class State:
|
|
| 47 |
|
| 48 |
# Helper functions
|
| 49 |
def remove_reasoning_from_output(output):
|
|
|
|
| 50 |
return output.split("</think>")[-1].strip()
|
| 51 |
|
| 52 |
def clean_json_tags(text):
|
|
|
|
| 53 |
return text.replace("```json\n", "").replace("\n```", "")
|
| 54 |
|
| 55 |
def clean_markdown_tags(text):
|
|
|
|
| 56 |
return text.replace("```markdown\n", "").replace("\n```", "")
|
| 57 |
|
| 58 |
def tavily_search(query, include_raw_content=True, max_results=3):
|
|
|
|
| 59 |
tavily_client = TavilyClient(api_key=config.TAVILY_API_KEY)
|
| 60 |
return tavily_client.search(query, include_raw_content=include_raw_content, max_results=max_results)
|
| 61 |
|
| 62 |
def update_state_with_search_results(search_results, idx_paragraph, state):
|
|
|
|
| 63 |
for search_result in search_results["results"]:
|
| 64 |
search = Search(url=search_result["url"], content=search_result["raw_content"])
|
| 65 |
state.paragraphs[idx_paragraph].research.search_history.append(search)
|
|
@@ -94,14 +98,11 @@ Only return the json object, no explanation or additional text.
|
|
| 94 |
|
| 95 |
class ReportStructureAgent:
|
| 96 |
def __init__(self, query: str):
|
| 97 |
-
self.
|
| 98 |
-
api_key=config.SAMBANOVA_API_KEY,
|
| 99 |
-
base_url=config.SAMBANOVA_BASE_URL
|
| 100 |
-
)
|
| 101 |
self.query = query
|
| 102 |
|
| 103 |
def run(self) -> str:
|
| 104 |
-
response = self.
|
| 105 |
model=config.LLM_REASONING,
|
| 106 |
messages=[
|
| 107 |
{"role": "system", "content": SYSTEM_PROMPT_REPORT_STRUCTURE},
|
|
@@ -157,13 +158,10 @@ Only return the json object, no explanation or additional text.
|
|
| 157 |
|
| 158 |
class FirstSearchAgent:
|
| 159 |
def __init__(self):
|
| 160 |
-
self.
|
| 161 |
-
api_key=config.SAMBANOVA_API_KEY,
|
| 162 |
-
base_url=config.SAMBANOVA_BASE_URL
|
| 163 |
-
)
|
| 164 |
|
| 165 |
def run(self, message) -> str:
|
| 166 |
-
response = self.
|
| 167 |
model=config.LLM_REGULAR,
|
| 168 |
messages=[
|
| 169 |
{"role": "system", "content": SYSTEM_PROMPT_FIRST_SEARCH},
|
|
@@ -216,13 +214,10 @@ Only return the json object, no explanation or additional text.
|
|
| 216 |
|
| 217 |
class FirstSummaryAgent:
|
| 218 |
def __init__(self):
|
| 219 |
-
self.
|
| 220 |
-
api_key=config.SAMBANOVA_API_KEY,
|
| 221 |
-
base_url=config.SAMBANOVA_BASE_URL
|
| 222 |
-
)
|
| 223 |
|
| 224 |
def run(self, message) -> str:
|
| 225 |
-
response = self.
|
| 226 |
model=config.LLM_REGULAR,
|
| 227 |
messages=[
|
| 228 |
{"role": "system", "content": SYSTEM_PROMPT_FIRST_SUMMARY},
|
|
@@ -281,13 +276,10 @@ Only return the json object, no explanation or additional text.
|
|
| 281 |
|
| 282 |
class ReflectionAgent:
|
| 283 |
def __init__(self):
|
| 284 |
-
self.
|
| 285 |
-
api_key=config.SAMBANOVA_API_KEY,
|
| 286 |
-
base_url=config.SAMBANOVA_BASE_URL
|
| 287 |
-
)
|
| 288 |
|
| 289 |
def run(self, message) -> str:
|
| 290 |
-
response = self.
|
| 291 |
model=config.LLM_REGULAR,
|
| 292 |
messages=[
|
| 293 |
{"role": "system", "content": SYSTEM_PROMPT_REFLECTION},
|
|
@@ -346,13 +338,10 @@ Only return the json object, no explanation or additional text.
|
|
| 346 |
|
| 347 |
class ReflectionSummaryAgent:
|
| 348 |
def __init__(self):
|
| 349 |
-
self.
|
| 350 |
-
api_key=config.SAMBANOVA_API_KEY,
|
| 351 |
-
base_url=config.SAMBANOVA_BASE_URL
|
| 352 |
-
)
|
| 353 |
|
| 354 |
def run(self, message) -> str:
|
| 355 |
-
response = self.
|
| 356 |
model=config.LLM_REGULAR,
|
| 357 |
messages=[
|
| 358 |
{"role": "system", "content": SYSTEM_PROMPT_REFLECTION_SUMMARY},
|
|
@@ -399,13 +388,10 @@ Use titles of the paragraphs to create a title for the report.
|
|
| 399 |
|
| 400 |
class ReportFormattingAgent:
|
| 401 |
def __init__(self):
|
| 402 |
-
self.
|
| 403 |
-
api_key=config.SAMBANOVA_API_KEY,
|
| 404 |
-
base_url=config.SAMBANOVA_BASE_URL
|
| 405 |
-
)
|
| 406 |
|
| 407 |
def run(self, message) -> str:
|
| 408 |
-
response = self.
|
| 409 |
model=config.LLM_REASONING,
|
| 410 |
messages=[
|
| 411 |
{"role": "system", "content": SYSTEM_PROMPT_REPORT_FORMATTING},
|
|
@@ -418,7 +404,7 @@ class ReportFormattingAgent:
|
|
| 418 |
return summary
|
| 419 |
|
| 420 |
# Streamlit Web Interface
|
| 421 |
-
st.title("Research Agent")
|
| 422 |
|
| 423 |
query = st.text_input("Enter your research query:", "")
|
| 424 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
| 3 |
import json
|
| 4 |
from dataclasses import dataclass, field
|
| 5 |
from typing import List
|
| 6 |
+
from groq import Groq
|
| 7 |
from json.decoder import JSONDecodeError
|
| 8 |
from pydantic_settings import BaseSettings
|
| 9 |
+
from tavily import TavilyClient
|
| 10 |
|
| 11 |
# Configuration class for API keys and settings
|
| 12 |
class Config(BaseSettings):
|
| 13 |
+
GROQ_API_KEY: str
|
| 14 |
+
LLM_REASONING: str = "llama3-70b-8192" # Example Groq model for reasoning tasks
|
| 15 |
+
LLM_REGULAR: str = "llama3-8b-8192" # Example Groq model for regular tasks
|
|
|
|
| 16 |
TAVILY_API_KEY: str
|
| 17 |
|
| 18 |
# Load configuration from environment variables
|
| 19 |
config = Config(
|
| 20 |
+
GROQ_API_KEY=os.environ["GROQ_API_KEY"],
|
| 21 |
TAVILY_API_KEY=os.environ["TAVILY_API_KEY"]
|
| 22 |
)
|
| 23 |
|
|
|
|
| 46 |
|
| 47 |
# Helper functions
|
| 48 |
def remove_reasoning_from_output(output):
|
| 49 |
+
"""Remove reasoning tags from output if present."""
|
| 50 |
return output.split("</think>")[-1].strip()
|
| 51 |
|
| 52 |
def clean_json_tags(text):
|
| 53 |
+
"""Remove JSON markdown tags from text."""
|
| 54 |
return text.replace("```json\n", "").replace("\n```", "")
|
| 55 |
|
| 56 |
def clean_markdown_tags(text):
|
| 57 |
+
"""Remove markdown tags from text."""
|
| 58 |
return text.replace("```markdown\n", "").replace("\n```", "")
|
| 59 |
|
| 60 |
def tavily_search(query, include_raw_content=True, max_results=3):
|
| 61 |
+
"""Perform a web search using Tavily."""
|
| 62 |
tavily_client = TavilyClient(api_key=config.TAVILY_API_KEY)
|
| 63 |
return tavily_client.search(query, include_raw_content=include_raw_content, max_results=max_results)
|
| 64 |
|
| 65 |
def update_state_with_search_results(search_results, idx_paragraph, state):
|
| 66 |
+
"""Update the state with search results."""
|
| 67 |
for search_result in search_results["results"]:
|
| 68 |
search = Search(url=search_result["url"], content=search_result["raw_content"])
|
| 69 |
state.paragraphs[idx_paragraph].research.search_history.append(search)
|
|
|
|
| 98 |
|
| 99 |
class ReportStructureAgent:
|
| 100 |
def __init__(self, query: str):
|
| 101 |
+
self.groq_client = Groq(api_key=config.GROQ_API_KEY)
|
|
|
|
|
|
|
|
|
|
| 102 |
self.query = query
|
| 103 |
|
| 104 |
def run(self) -> str:
|
| 105 |
+
response = self.groq_client.chat.completions.create(
|
| 106 |
model=config.LLM_REASONING,
|
| 107 |
messages=[
|
| 108 |
{"role": "system", "content": SYSTEM_PROMPT_REPORT_STRUCTURE},
|
|
|
|
| 158 |
|
| 159 |
class FirstSearchAgent:
|
| 160 |
def __init__(self):
|
| 161 |
+
self.groq_client = Groq(api_key=config.GROQ_API_KEY)
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
def run(self, message) -> str:
|
| 164 |
+
response = self.groq_client.chat.completions.create(
|
| 165 |
model=config.LLM_REGULAR,
|
| 166 |
messages=[
|
| 167 |
{"role": "system", "content": SYSTEM_PROMPT_FIRST_SEARCH},
|
|
|
|
| 214 |
|
| 215 |
class FirstSummaryAgent:
|
| 216 |
def __init__(self):
|
| 217 |
+
self.groq_client = Groq(api_key=config.GROQ_API_KEY)
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
def run(self, message) -> str:
|
| 220 |
+
response = self.groq_client.chat.completions.create(
|
| 221 |
model=config.LLM_REGULAR,
|
| 222 |
messages=[
|
| 223 |
{"role": "system", "content": SYSTEM_PROMPT_FIRST_SUMMARY},
|
|
|
|
| 276 |
|
| 277 |
class ReflectionAgent:
|
| 278 |
def __init__(self):
|
| 279 |
+
self.groq_client = Groq(api_key=config.GROQ_API_KEY)
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
def run(self, message) -> str:
|
| 282 |
+
response = self.groq_client.chat.completions.create(
|
| 283 |
model=config.LLM_REGULAR,
|
| 284 |
messages=[
|
| 285 |
{"role": "system", "content": SYSTEM_PROMPT_REFLECTION},
|
|
|
|
| 338 |
|
| 339 |
class ReflectionSummaryAgent:
|
| 340 |
def __init__(self):
|
| 341 |
+
self.groq_client = Groq(api_key=config.GROQ_API_KEY)
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
def run(self, message) -> str:
|
| 344 |
+
response = self.groq_client.chat.completions.create(
|
| 345 |
model=config.LLM_REGULAR,
|
| 346 |
messages=[
|
| 347 |
{"role": "system", "content": SYSTEM_PROMPT_REFLECTION_SUMMARY},
|
|
|
|
| 388 |
|
| 389 |
class ReportFormattingAgent:
|
| 390 |
def __init__(self):
|
| 391 |
+
self.groq_client = Groq(api_key=config.GROQ_API_KEY)
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
def run(self, message) -> str:
|
| 394 |
+
response = self.groq_client.chat.completions.create(
|
| 395 |
model=config.LLM_REASONING,
|
| 396 |
messages=[
|
| 397 |
{"role": "system", "content": SYSTEM_PROMPT_REPORT_FORMATTING},
|
|
|
|
| 404 |
return summary
|
| 405 |
|
| 406 |
# Streamlit Web Interface
|
| 407 |
+
st.title("Research Agent with Groq")
|
| 408 |
|
| 409 |
query = st.text_input("Enter your research query:", "")
|
| 410 |
|