3
3
from langchain_google_genai import ChatGoogleGenerativeAI
4
4
from langchain .prompts import PromptTemplate
5
5
from langchain_core .output_parsers import StrOutputParser
6
- from langchain_community .tools import DuckDuckGoSearchRun
7
6
from Searcher import Searcher
8
7
import requests
9
- from get_github_content import get_github_file_content
8
+ import base64
9
+ import asyncio
10
10
11
11
load_dotenv ()
12
12
GOOGLE_API_KEY = os .getenv ("GOOGLE_API_KEY" )
13
-
13
+ ghtoken = ""
14
14
def create_search_qns (question , context ):
15
15
16
16
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.
17
17
These questions are used to guide the user to the answer they are looking for. Keep that in mind.
18
18
NOTE:1) You are not allowed to ask questions that are already asked.
19
19
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.
22
22
5) your questions must delve deeper into the topic based on the given question and the depth asked.
23
23
6) The output must be in a list format.
24
24
@@ -30,6 +30,8 @@ def create_search_qns(question, context):
30
30
31
31
chain = SQprompt | llm | StrOutputParser ()
32
32
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 ]
33
35
return search_qns
34
36
35
37
@@ -50,81 +52,42 @@ def create_search_qns(question, context):
50
52
51
53
searcher = Searcher ()
52
54
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
-
89
55
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 ()
90
60
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"
91
69
92
- # llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=GOOGLE_API_KEY)
93
70
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 :
120
73
if (question == "exit" ):
121
74
return "Goodbye"
122
- gitfile = get_github_file_content (codeurl )
75
+ gitfile = get_github_file_content (codeurl , ghtoken )
123
76
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 ("------------------------------------------------------" )
125
87
response = chain .invoke ({"question" : question , "context" :gitfile , "chathistory" : chathistory [:10 ], "searchresults" : searchresults })
126
88
chathistory .append ((f"Human: { question } " , f"AI: { response } " , f"Search Results: { searchresults } " ))
127
- # print("------------------------------------------------------")
128
- # print(response)
129
- # print("------------------------------------------------------")
89
+ # return ''
130
90
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