Spaces:
Sleeping
Sleeping
File size: 5,897 Bytes
26f4ce6 e6faee1 0c57baf e6faee1 26f4ce6 ad47bff a524035 112bb85 04edc96 112bb85 c252a6d 26f4ce6 f522766 26f4ce6 9f5931f d56f58a 26f4ce6 9f5931f 26f4ce6 cfa71fe |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
import requests
from bs4 import BeautifulSoup
from langchain.schema.runnable import RunnablePassthrough, RunnableLambda
from langchain.utilities import DuckDuckGoSearchAPIWrapper
import json
from urllib3.exceptions import NewConnectionError
from langchain_community.document_loaders import UnstructuredURLLoader
from langchain_google_genai import GoogleGenerativeAI
from langchain_core.prompts import PromptTemplate
import os
import openai
import sys
import gradio as gr
from langchain_community.utilities import GoogleSearchAPIWrapper
from langchain_core.tools import Tool
from huggingface_hub import HfApi
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
sys.path.append('../..')
os.environ["OPENAI_API_KEY"]='sk-proj-ukx0nfRmfiOHojgJAvsBT3BlbkFJhYsVcTGHeqtTdCYXZvUD'
openai.api_key = os.environ["OPENAI_API_KEY"]
api_key='AIzaSyAK26dJHr6PtqjxYGwb052NgIMMITbii2E'
os.environ["GOOGLE_API_KEY"]="AIzaSyAK26dJHr6PtqjxYGwb052NgIMMITbii2E"
os.environ["GOOGLE_CSE_ID"]="a64e58dbd396042ed"
llm = GoogleGenerativeAI(model="gemini-pro", google_api_key=api_key)
RESULTS_PER_QUESTION = 3
ddg_search = DuckDuckGoSearchAPIWrapper()
def web_search(query: str, num_results: int = RESULTS_PER_QUESTION):
results = ddg_search.results(query, num_results)
return [r["link"] for r in results]
def scrape_text(url: str):
# Send a GET request to the webpage
try:
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Parse the content of the request with BeautifulSoup
soup = BeautifulSoup(response.text, "html.parser")
# Extract all text from the webpage
page_text = soup.get_text(separator=" ", strip=True)
# Print the extracted text
return page_text
else:
return f"Failed to retrieve the webpage: Status code {response.status_code}"
except Exception as e:
print(e)
return f"Failed to retrieve the webpage: {e}"
SUMMARY_TEMPLATE = """{text}
-----------
Using the above text, answer in short the following question:
> {question}
-----------
if the question cannot be answered using the text, imply summarize the text. Include all factual information, numbers, stats etc if available."""
SUMMARY_PROMPT = ChatPromptTemplate.from_template(SUMMARY_TEMPLATE)
scrape_and_summarize_chain = RunnablePassthrough.assign(
summary=RunnablePassthrough.assign(
text=lambda x: scrape_text(x["url"])[:10000]
) | SUMMARY_PROMPT | llm | StrOutputParser() # Use GeminiPro with temperature adjustment
) | (lambda x: f"URL: {x['url']}\n\nSUMMARY: {x['summary']}")
web_search_chain = RunnablePassthrough.assign(
urls=lambda x: web_search(x["question"])
) | (lambda x: [{"question": x["question"], "url": u} for u in x["urls"]]) | scrape_and_summarize_chain.map()
SEARCH_PROMPT = ChatPromptTemplate.from_messages(
[
(
"user",
"Write 3 google search queries to search online that form an "
"objective opinion from the following: {question}\n"
"You must respond with a list of strings in the following format: "
'["query 1", "query 2", "query 3"].',
),
]
)
search_question_chain = SEARCH_PROMPT | llm | StrOutputParser() | json.loads
full_research_chain = search_question_chain | (lambda x: [{"question": q} for q in x]) | web_search_chain.map()
WRITER_SYSTEM_PROMPT = "You are an AI Shopping Assistants. Your sole purpose is to write well written, critically acclaimed, objective and structured reports on given text." # noqa: E501
# Report prompts from https://github.com/assafelovic/gpt-researcher/blob/master/gpt_researcher/master/prompts.py
RESEARCH_REPORT_TEMPLATE = """Information:
--------
{research_summary}
--------
Using the above information, answer the following question or topic: "{question}" in a detailed report -- \
The report should focus on the answer to the question, should be well structured, informative, \
in depth, with facts,baterry and processor benchmarks,prices of the products in all stores and Write price in each store and write the name of the store and check if there were any offers and a minimum of 200 words.
You should compare between the product in question and products in the same price range.
You should suggest the best products based on price range in the question and compare between them.
You should strive to write the report as long as you can using all relevant and necessary information provided.
You must write the report with markdown syntax.
You MUST determine your own concrete and valid opinion based on the given information. Do NOT deter to general and meaningless conclusions.
Write all used source urls at the end of the report, and make sure to not add duplicated sources, but only one reference for each.
You must write the report in apa format.
Please do your best, this is very important to my opinion about product."""
prompt = ChatPromptTemplate.from_messages(
[
("system", WRITER_SYSTEM_PROMPT),
("user", RESEARCH_REPORT_TEMPLATE),
]
)
def collapse_list_of_lists(list_of_lists):
content = []
for l in list_of_lists:
content.append("\n\n".join(l))
return "\n\n".join(content)
chain = RunnablePassthrough.assign(
research_summary= full_research_chain | collapse_list_of_lists
) | prompt | llm | StrOutputParser()
def predict(input, history):
response= chain.invoke({"question":input})
return str(response)
gr.ChatInterface(predict,description="These assistants could help customers find products, compare options,answer questions and recommend the best product based on customer needs.").launch(share=True) |