Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -7,7 +7,7 @@ import os
|
|
| 7 |
from dotenv import load_dotenv, find_dotenv
|
| 8 |
|
| 9 |
# Load environment variables from .env file
|
| 10 |
-
#
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
|
|
@@ -25,18 +25,39 @@ SysPromptList = "You are now in the role of an expert AI who can extract structu
|
|
| 25 |
SysPromptDefault = "You are an expert AI, complete the given task. Do not add any additional comments."
|
| 26 |
SysPromptMd = "You are an expert AI who can create a structured report using information provided in the context from user request.The report should be in markdown format consists of markdown tables structured into subtopics. Do not add any additional comments."
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
query: str = Query(default="market research", description="input query to generate Report")
|
| 30 |
description: str = Query(default="", description="additional context for report")
|
| 31 |
user_id: str = Query(default="", description="unique user id")
|
| 32 |
user_name: str = Query(default="", description="user name")
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
@app.post("/generate_report")
|
| 35 |
-
async def generate_report(request: Request, query:
|
| 36 |
query_str = query.query
|
| 37 |
description = query.description
|
| 38 |
user_id = query.user_id
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
# Combine query with user keywords
|
| 41 |
search_query = query_str
|
| 42 |
|
|
@@ -44,7 +65,7 @@ async def generate_report(request: Request, query: Query):
|
|
| 44 |
urls = search_brave(search_query, num_results=4)
|
| 45 |
|
| 46 |
# Fetch and extract content from the URLs
|
| 47 |
-
all_text_with_urls = fetch_and_extract_content(urls, query_str)
|
| 48 |
|
| 49 |
# Prepare the prompt for generating the report
|
| 50 |
additional_context = limit_tokens(str(all_text_with_urls))
|
|
@@ -52,7 +73,7 @@ async def generate_report(request: Request, query: Query):
|
|
| 52 |
md_report = together_response(prompt, model=llm_default_medium, SysPrompt=SysPromptMd)
|
| 53 |
|
| 54 |
# Insert data into database (or other storage)
|
| 55 |
-
insert_data(user_id, query_str, description, str(all_text_with_urls), md_report)
|
| 56 |
references_html = dict()
|
| 57 |
for text, url in all_text_with_urls:
|
| 58 |
references_html[url] = str(md_to_html(text))
|
|
@@ -69,5 +90,4 @@ app.add_middleware(
|
|
| 69 |
allow_origins=["*"],
|
| 70 |
allow_credentials=True,
|
| 71 |
allow_methods=["*"],
|
| 72 |
-
allow_headers=["*"],
|
| 73 |
-
)
|
|
|
|
| 7 |
from dotenv import load_dotenv, find_dotenv
|
| 8 |
|
| 9 |
# Load environment variables from .env file
|
| 10 |
+
#load_dotenv("keys.env")
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
|
|
|
|
| 25 |
SysPromptDefault = "You are an expert AI, complete the given task. Do not add any additional comments."
|
| 26 |
SysPromptMd = "You are an expert AI who can create a structured report using information provided in the context from user request.The report should be in markdown format consists of markdown tables structured into subtopics. Do not add any additional comments."
|
| 27 |
|
| 28 |
+
sys_prompts = {
|
| 29 |
+
"offline": {
|
| 30 |
+
"Chat": "You are an expert AI, complete the given task. Do not add any additional comments.",
|
| 31 |
+
"Full Text Report": "You are an expert AI who can create a detailed report from user request. The report should be in markdown format. Do not add any additional comments.",
|
| 32 |
+
"Tabular Report": "You are an expert AI who can create a structured report from user request.The report should be in markdown format structured into subtopics/tables/lists. Do not add any additional comments.",
|
| 33 |
+
"Tables only": "You are an expert AI who can create a structured tabular report from user request.The report should be in markdown format consists of only markdown tables. Do not add any additional comments.",
|
| 34 |
+
},
|
| 35 |
+
"online": {
|
| 36 |
+
"Chat": "You are an expert AI, complete the given task using the provided context. Do not add any additional comments.",
|
| 37 |
+
"Full Text Report": "You are an expert AI who can create a detailed report using information provided in the context from user request. The report should be in markdown format. Do not add any additional comments.",
|
| 38 |
+
"Tabular Report": "You are an expert AI who can create a structured report using information provided in the context from user request. The report should be in markdown format structured into subtopics/tables/lists. Do not add any additional comments.",
|
| 39 |
+
"Tables only": "You are an expert AI who can create a structured tabular report using information provided in the context from user request. The report should be in markdown format consists of only markdown tables. Do not add any additional comments.",
|
| 40 |
+
},
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
class QueryModel(BaseModel):
|
| 44 |
query: str = Query(default="market research", description="input query to generate Report")
|
| 45 |
description: str = Query(default="", description="additional context for report")
|
| 46 |
user_id: str = Query(default="", description="unique user id")
|
| 47 |
user_name: str = Query(default="", description="user name")
|
| 48 |
+
internet: bool = Query(default=True, description="Enable Internet search")
|
| 49 |
+
output_format: str = Query(default="Tabular Report", description="Output format for the report", enum=["Chat", "Full Text Report", "Tabular Report", "Tables only"])
|
| 50 |
+
data_format: str = Query(default="Structured data", description="Type of data to extract from the internet", enum=["No presets", "Structured data", "Quantitative data"])
|
| 51 |
|
| 52 |
@app.post("/generate_report")
|
| 53 |
+
async def generate_report(request: Request, query: QueryModel):
|
| 54 |
query_str = query.query
|
| 55 |
description = query.description
|
| 56 |
user_id = query.user_id
|
| 57 |
+
internet = "online" if query.internet else "offline"
|
| 58 |
+
output_format = sys_prompts[internet][query.output_format]
|
| 59 |
+
data_format = query.data_format
|
| 60 |
+
#return {output_format,data_format}
|
| 61 |
# Combine query with user keywords
|
| 62 |
search_query = query_str
|
| 63 |
|
|
|
|
| 65 |
urls = search_brave(search_query, num_results=4)
|
| 66 |
|
| 67 |
# Fetch and extract content from the URLs
|
| 68 |
+
all_text_with_urls = fetch_and_extract_content(data_format, urls, query_str)
|
| 69 |
|
| 70 |
# Prepare the prompt for generating the report
|
| 71 |
additional_context = limit_tokens(str(all_text_with_urls))
|
|
|
|
| 73 |
md_report = together_response(prompt, model=llm_default_medium, SysPrompt=SysPromptMd)
|
| 74 |
|
| 75 |
# Insert data into database (or other storage)
|
| 76 |
+
#insert_data(user_id, query_str, description, str(all_text_with_urls), md_report)
|
| 77 |
references_html = dict()
|
| 78 |
for text, url in all_text_with_urls:
|
| 79 |
references_html[url] = str(md_to_html(text))
|
|
|
|
| 90 |
allow_origins=["*"],
|
| 91 |
allow_credentials=True,
|
| 92 |
allow_methods=["*"],
|
| 93 |
+
allow_headers=["*"],)
|
|
|