Skip to content

Commit 67ef604

Browse files
changed main.py
1 parent 157c451 commit 67ef604

7 files changed

+63
-104
lines changed

requirements.txt LLMreq.txt

File renamed without changes.

backend/.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
GOOGLE_API_KEY = AIzaSyC-qn-WYa3Fd_pgo9DHl4OtqT9wmOY977s
1+
GOOGLE_API_KEY = AIzaSyCZygRCJvAVuvReURltzdVOVgyT_JUeLJU

backend/Searcher.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,31 @@
55

66
class Searcher:
77
async def aget_results(self, word):
8-
results = await AsyncDDGS(proxies=None).text(word, max_results=5)
8+
results = await AsyncDDGS(proxies=None).text(word, max_results=2)
99
links = [result['href'] for result in results]
1010
return links
1111

1212
def get_content(self, url):
13-
response = requests.get(url)
13+
try:
14+
response = requests.get(url)
15+
response.raise_for_status() # Raise an exception if the response contains an HTTP error status code
16+
except requests.exceptions.RequestException as e:
17+
print(f"Error getting content from {url}: {e}")
18+
return ""
19+
1420
return response.text
1521

1622
def parse_html(self, content) -> str:
23+
# soup = BeautifulSoup(content, 'html.parser')
24+
# text = soup.get_text()
25+
# text.replace("\n", " ")
26+
# return text
1727
soup = BeautifulSoup(content, 'html.parser')
18-
text = soup.get_text()
19-
text.replace("\n\n", "\n")
20-
return text
28+
paragraphs = soup.find_all('p')
29+
text = ' '.join(paragraph.get_text() for paragraph in paragraphs)
30+
words = text.split()
31+
truncated_text = ' '.join(words[:500])
32+
return truncated_text
2133

2234
async def search_and_get_links(self, word):
2335
links = await self.aget_results(word)
@@ -31,3 +43,12 @@ async def search_and_get_content(self, word):
3143
parsed_content = self.parse_html(content)
3244
parsed_contents.append(parsed_content)
3345
return parsed_contents
46+
47+
48+
searcher = Searcher()
49+
async def main():
50+
a1 = await searcher.search_and_get_links("how to create a class in python")
51+
a2 = await searcher.search_and_get_content("how to create a class in python")
52+
return a1, a2
53+
54+
print(asyncio.run(main()))
1.34 KB
Binary file not shown.
Binary file not shown.

backend/get_github_content.py

-25
This file was deleted.

backend/main.py

+36-73
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
from langchain_google_genai import ChatGoogleGenerativeAI
44
from langchain.prompts import PromptTemplate
55
from langchain_core.output_parsers import StrOutputParser
6-
from langchain_community.tools import DuckDuckGoSearchRun
76
from Searcher import Searcher
87
import requests
9-
from get_github_content import get_github_file_content
8+
import base64
9+
import asyncio
1010

1111
load_dotenv()
1212
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
13-
13+
ghtoken = ""
1414
def create_search_qns(question, context):
1515

1616
SQprompt = PromptTemplate.from_template("""You are an expert question asker, Now your task is to ask questions which expand upon a question given using the context (which is most probably a code file) as reference and guide.
1717
These questions are used to guide the user to the answer they are looking for. Keep that in mind.
1818
NOTE:1) You are not allowed to ask questions that are already asked.
1919
2) your questions must be relevant to the given question.
20-
3) you are allowed to ask a maximum of 15 questions.
21-
4) you must ask a minimum of 3 questions.
20+
3) you are allowed to ask a maximum of 4 questions.
21+
4) you must ask a minimum of 2 questions.
2222
5) your questions must delve deeper into the topic based on the given question and the depth asked.
2323
6) The output must be in a list format.
2424
@@ -30,6 +30,8 @@ def create_search_qns(question, context):
3030

3131
chain = SQprompt | llm | StrOutputParser()
3232
search_qns = chain.invoke({"question": question, "context": context})
33+
search_qns = search_qns.split("\n")
34+
search_qns = [qn for qn in search_qns if qn]
3335
return search_qns
3436

3537

@@ -50,81 +52,42 @@ def create_search_qns(question, context):
5052

5153
searcher = Searcher()
5254

53-
def get_github_file_content(url):
54-
response = requests.get(url)
55-
return response.text
56-
57-
# gitfile="""import os
58-
# from dotenv import load_dotenv
59-
# from langchain_google_genai import ChatGoogleGenerativeAI
60-
# from langchain.prompts import PromptTemplate
61-
# from langchain_core.output_parsers import StrOutputParser
62-
# from langchain.agents import initialize_agent, AgentType
63-
# from langchain.tools import Tool
64-
# from langchain_community.tools import DuckDuckGoSearchResults
65-
66-
# import requests
67-
# from bs4 import BeautifulSoup
68-
69-
70-
# load_dotenv()
71-
# GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
72-
73-
74-
# ddg_search = DuckDuckGoSearchResults()
75-
# HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
76-
77-
# def parse_html(content) -> str:
78-
# soup = BeautifulSoup(content, 'html.parser')
79-
# return soup.get_text()
80-
81-
# def fetch_web_page(url: str) -> str:
82-
# response = requests.get(url, headers=HEADERS)
83-
# return parse_html(response.content)
84-
85-
# web_fetcher_tool = Tool.from_function(func=fetch_web_page,
86-
# name="web_fetcher",
87-
# description="Fetches the content of a web page and returns it as a string.")
88-
8955

56+
def get_github_file_content(url:str, token:str):
57+
headers = {'Authorization': f'token {token}'}
58+
response = requests.get(url, headers=headers)
59+
response_json = response.json()
9060

61+
if 'content' in response_json:
62+
content_base64 = response_json['content']
63+
content = base64.b64decode(content_base64).decode('utf-8')
64+
return content
65+
elif 'message' in response_json:
66+
return response_json['message']
67+
else:
68+
return "Error: Unexpected response"
9169

92-
# llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=GOOGLE_API_KEY)
9370

94-
95-
# prompt = PromptTemplate.from_template("Summarize the following content: {content}")
96-
97-
# chain = prompt | llm | StrOutputParser()
98-
99-
# summarizer_tool = Tool.from_function(func=chain.invoke,
100-
# name="summarizer",
101-
# description="Summarizes the content of a web page and returns it as a string.")
102-
103-
# tools = [ddg_search, web_fetcher_tool, summarizer_tool]
104-
105-
# agent = initialize_agent(
106-
# tools = tools,
107-
# agent_type = AgentType.ZERO_SHOT_REACT_DESCRIPTION,
108-
# llm = llm,
109-
# verbose = True
110-
# )
111-
112-
# if __name__ == "__main__":
113-
# # content = "Research how to use the requests library in Python. Use your tools to search and summarize content into a guide on how to use the requests library."
114-
# # print(agent.invoke({"input": content}))
115-
# """
116-
117-
118-
chathistory = []
119-
def main(question:str, codeurl) -> str:
71+
chathistory=[]
72+
async def main(question:str, codeurl) -> str:
12073
if (question=="exit"):
12174
return "Goodbye"
122-
gitfile = get_github_file_content(codeurl)
75+
gitfile = get_github_file_content(codeurl, ghtoken)
12376
searchquestions = create_search_qns(question, gitfile)
124-
searchresults = searcher.search_and_get_content(searchquestions)
77+
print("------------------------------------------------------")
78+
print(searchquestions)
79+
print("------------------------------------------------------")
80+
searchresults = {}
81+
for qn in searchquestions:
82+
searchresults[qn] = await searcher.search_and_get_content(qn)
83+
print(f"{await searcher.search_and_get_content(qn)}\n\n\n\n\n")
84+
print("------------------------------------------------------")
85+
print(searchresults)
86+
print("------------------------------------------------------")
12587
response = chain.invoke({"question": question, "context":gitfile, "chathistory": chathistory[:10], "searchresults": searchresults})
12688
chathistory.append((f"Human: {question}", f"AI: {response}", f"Search Results: {searchresults}"))
127-
# print("------------------------------------------------------")
128-
# print(response)
129-
# print("------------------------------------------------------")
89+
# return ''
13090
return response
91+
92+
93+
print(asyncio.run(main("what is this about?", "https://api.github.com/repos/yashwantherukulla/SpeakBot/contents/va_bing.py")))

0 commit comments

Comments
 (0)