From 782ace2878c22f6b51382cec58a1867b4f911fcf Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Thu, 19 Oct 2023 01:15:12 +0530 Subject: [PATCH 1/8] for retrieving wikipedia content --- model.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/model.py b/model.py index ff85249..599da2b 100644 --- a/model.py +++ b/model.py @@ -1,11 +1,22 @@ # import os import openai +from langchain.utilities import WikipediaAPIWrapper with open("API_KEY", 'r') as f: openai.api_key = f.read().strip() MODEL_ENGINE = "text-davinci-002" +question = "who is naruto" +def wiki_answer(question): + wiki= WikipediaAPIWrapper() + result = wiki.run(question) + + + + +print(wiki_answer(question)) + def generate_answer(question): prompt = (f"Question: {question}\n" "Answer:") From 8e6d84e85b9dd3e5a1e03b9b288762984b6b8b5b Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Thu, 19 Oct 2023 01:31:11 +0530 Subject: [PATCH 2/8] making a summary of the entire content with curated prompt --- model.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/model.py b/model.py index 599da2b..c435fe4 100644 --- a/model.py +++ b/model.py @@ -1,25 +1,43 @@ # import os import openai from langchain.utilities import WikipediaAPIWrapper +from langchain import OpenAI +from langchain import PromptTemplate +import os +from dotenv import find_dotenv, load_dotenv -with open("API_KEY", 'r') as f: - openai.api_key = f.read().strip() +# with open("API_KEY", "r") as f: +# openai.api_key = f.read().strip() MODEL_ENGINE = "text-davinci-002" +load_dotenv(find_dotenv()) +OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") question = "who is naruto" + + def wiki_answer(question): - wiki= WikipediaAPIWrapper() + wiki = WikipediaAPIWrapper() + + llm = OpenAI(temperature=0.5) result = wiki.run(question) - + template = """ + Please write a concise and to the point summary, make it easy to understand, include all important points from the following text: + {result} + Write only those points whhich are related to the {question} +""" + prompt = PromptTemplate(input_variables=["result","question"], template=template) + summary_prompt = prompt.format(result=result,question=question) + summary = llm(summary_prompt) + return summary print(wiki_answer(question)) + def generate_answer(question): - prompt = (f"Question: {question}\n" - "Answer:") + prompt = f"Question: {question}\n" "Answer:" completions = openai.Completion.create( engine=MODEL_ENGINE, prompt=prompt, From 696562ba980c80b113720b75c93cf2d3a1b04e95 Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Thu, 19 Oct 2023 01:38:04 +0530 Subject: [PATCH 3/8] python REPL to execute arbitrary code~ --- model.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/model.py b/model.py index c435fe4..12f1db4 100644 --- a/model.py +++ b/model.py @@ -5,7 +5,7 @@ from langchain import PromptTemplate import os from dotenv import find_dotenv, load_dotenv - +from langchain.utilities import PythonREPL # with open("API_KEY", "r") as f: # openai.api_key = f.read().strip() MODEL_ENGINE = "text-davinci-002" @@ -13,10 +13,10 @@ load_dotenv(find_dotenv()) OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") -question = "who is naruto" +question = print('HEllo langchain') -def wiki_answer(question): +def ask_wiki(question): wiki = WikipediaAPIWrapper() llm = OpenAI(temperature=0.5) @@ -33,7 +33,12 @@ def wiki_answer(question): return summary -print(wiki_answer(question)) +def ask_REPL(question): + python_repl = PythonREPL() + result = python_repl.run(question) + + +print(ask_REPL(question)) def generate_answer(question): From 3d86ab1e5c989537916d37bfa4ec2e1163c7e2f2 Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Thu, 19 Oct 2023 01:45:30 +0530 Subject: [PATCH 4/8] querying the web for certain topics --- model.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/model.py b/model.py index 12f1db4..2daa0af 100644 --- a/model.py +++ b/model.py @@ -6,6 +6,7 @@ import os from dotenv import find_dotenv, load_dotenv from langchain.utilities import PythonREPL +from langchain.tools import DuckDuckGoSearchRun # with open("API_KEY", "r") as f: # openai.api_key = f.read().strip() MODEL_ENGINE = "text-davinci-002" @@ -13,7 +14,7 @@ load_dotenv(find_dotenv()) OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") -question = print('HEllo langchain') +question = "Who won today's cwc match" def ask_wiki(question): @@ -36,9 +37,26 @@ def ask_wiki(question): def ask_REPL(question): python_repl = PythonREPL() result = python_repl.run(question) + return result -print(ask_REPL(question)) +def ask_web(question): + search = DuckDuckGoSearchRun() + result = search.run(question) + llm = OpenAI(temperature=0.5) + template = """ + Please write a concise and to the point summary, easy to understand, include all important points from the following text: + {result} + Write only those points which are related to the {question} +""" + prompt = PromptTemplate(input_variables=["result","question"], template=template) + summary_prompt = prompt.format(result=result,question=question) + summary = llm(summary_prompt) + + return summary + + +print(ask_web(question)) def generate_answer(question): From 96b3ea7cc22e428e4a0ed773e7e963d5096d1248 Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Thu, 19 Oct 2023 01:51:49 +0530 Subject: [PATCH 5/8] putting the functions together --- model.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/model.py b/model.py index 2daa0af..f7bbe85 100644 --- a/model.py +++ b/model.py @@ -7,6 +7,9 @@ from dotenv import find_dotenv, load_dotenv from langchain.utilities import PythonREPL from langchain.tools import DuckDuckGoSearchRun +from langchain.agents import Tool + + # with open("API_KEY", "r") as f: # openai.api_key = f.read().strip() MODEL_ENGINE = "text-davinci-002" @@ -16,11 +19,12 @@ question = "Who won today's cwc match" +llm = OpenAI(temperature=0.5) def ask_wiki(question): wiki = WikipediaAPIWrapper() - llm = OpenAI(temperature=0.5) + result = wiki.run(question) template = """ Please write a concise and to the point summary, make it easy to understand, include all important points from the following text: @@ -43,7 +47,7 @@ def ask_REPL(question): def ask_web(question): search = DuckDuckGoSearchRun() result = search.run(question) - llm = OpenAI(temperature=0.5) + template = """ Please write a concise and to the point summary, easy to understand, include all important points from the following text: {result} @@ -59,7 +63,10 @@ def ask_web(question): print(ask_web(question)) -def generate_answer(question): + + + +def ask_openai(question): prompt = f"Question: {question}\n" "Answer:" completions = openai.Completion.create( engine=MODEL_ENGINE, @@ -72,3 +79,12 @@ def generate_answer(question): message = completions.choices[0].text answer = message.strip().split("\nAnswer:")[-1].strip() return answer + + +tools = [ + Tool( + name = "wikipedia", + func= ask_wiki, + description="Useful for when you need to look up a topic, country or person on wikipedia" + ) +] \ No newline at end of file From 84c2d6ff3297cf985d1322111b0f2b3df51c011c Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Thu, 19 Oct 2023 02:03:34 +0530 Subject: [PATCH 6/8] appending all the tools --- model.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/model.py b/model.py index f7bbe85..1511c0d 100644 --- a/model.py +++ b/model.py @@ -85,6 +85,29 @@ def ask_openai(question): Tool( name = "wikipedia", func= ask_wiki, - description="Useful for when you need to look up a topic, country or person on wikipedia" + description=" Useful when you need factual information about topics, concepts, events and people. on wikipedia" ) -] \ No newline at end of file +] + +repl_tool = Tool( + name='Python REPL', + func= ask_REPL, + description="useful for when you need to use python to answer a question. You should input python code" + ) + +duckduckgo_tool = Tool( + name='Web Search', + func= ask_web, + description="Useful for when you need to do a search on the internet to find information that another tool can't find, or when you need up-to-date factual information that goes beyond your existing knowledge. be specific with your input." +) + +openai_tool = Tool( + name="openai search", + func= ask_openai, + description='Useful when you need accurate and insightful answers to complex questions that go beyond factual information.' +) + +tools.append(duckduckgo_tool) +tools.append(repl_tool) +tools.append(openai_tool) + From 70a6d66f58cd9ef0619abc32e5bd2b33407e2540 Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Thu, 19 Oct 2023 02:09:41 +0530 Subject: [PATCH 7/8] initialsing an agent which determines which tool to use --- model.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/model.py b/model.py index 1511c0d..f256b24 100644 --- a/model.py +++ b/model.py @@ -8,7 +8,7 @@ from langchain.utilities import PythonREPL from langchain.tools import DuckDuckGoSearchRun from langchain.agents import Tool - +from langchain.agents import initialize_agent # with open("API_KEY", "r") as f: # openai.api_key = f.read().strip() @@ -17,7 +17,7 @@ load_dotenv(find_dotenv()) OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") -question = "Who won today's cwc match" +question = "Who won the fight between Logan Paul and Dillon Danis And How?" llm = OpenAI(temperature=0.5) @@ -52,6 +52,7 @@ def ask_web(question): Please write a concise and to the point summary, easy to understand, include all important points from the following text: {result} Write only those points which are related to the {question} + Do NOT write unrelated points """ prompt = PromptTemplate(input_variables=["result","question"], template=template) summary_prompt = prompt.format(result=result,question=question) @@ -60,7 +61,7 @@ def ask_web(question): return summary -print(ask_web(question)) +# print(ask_web(question)) @@ -111,3 +112,13 @@ def ask_openai(question): tools.append(repl_tool) tools.append(openai_tool) +zero_shot_agent = initialize_agent( + agent="zero-shot-react-description", + tools=tools, + llm=llm, + verbose=True, + max_iterations=3, +) + + +answer = zero_shot_agent.run(question) From c828d7cc1aa3f7fc3df05db638df6e75c7e75732 Mon Sep 17 00:00:00 2001 From: Raj Aryan Singh Date: Thu, 19 Oct 2023 02:17:02 +0530 Subject: [PATCH 8/8] model works perfectly fine --- main.py | 4 +- model.py | 187 +++++++++++++++++++++++++++---------------------------- 2 files changed, 95 insertions(+), 96 deletions(-) diff --git a/main.py b/main.py index 662ee0f..d592520 100644 --- a/main.py +++ b/main.py @@ -19,7 +19,7 @@ def interact(username: str) -> bool: return False if 'my name' in question.lower(): question = 'My name is ' + username + '.' + question - answer = model.generate_answer(question) + answer = model.askme(question) print("Answer:", answer) speech.say(answer) return True @@ -34,7 +34,7 @@ def talk(username: str) -> bool: return False if 'my name' in question.lower(): question = 'My name is ' + username + '.' + question - answer = model.generate_answer(question) + answer = model.askme(question) print("Answer:", answer) speech.say(answer) return True diff --git a/model.py b/model.py index f256b24..7aa67c0 100644 --- a/model.py +++ b/model.py @@ -21,104 +21,103 @@ llm = OpenAI(temperature=0.5) -def ask_wiki(question): - wiki = WikipediaAPIWrapper() - - - result = wiki.run(question) - template = """ - Please write a concise and to the point summary, make it easy to understand, include all important points from the following text: - {result} - Write only those points whhich are related to the {question} -""" - prompt = PromptTemplate(input_variables=["result","question"], template=template) - summary_prompt = prompt.format(result=result,question=question) - summary = llm(summary_prompt) - - return summary - - -def ask_REPL(question): - python_repl = PythonREPL() - result = python_repl.run(question) - return result - - -def ask_web(question): - search = DuckDuckGoSearchRun() - result = search.run(question) - - template = """ - Please write a concise and to the point summary, easy to understand, include all important points from the following text: - {result} - Write only those points which are related to the {question} - Do NOT write unrelated points -""" - prompt = PromptTemplate(input_variables=["result","question"], template=template) - summary_prompt = prompt.format(result=result,question=question) - summary = llm(summary_prompt) - - return summary - - -# print(ask_web(question)) - - - - - -def ask_openai(question): - prompt = f"Question: {question}\n" "Answer:" - completions = openai.Completion.create( - engine=MODEL_ENGINE, - prompt=prompt, - max_tokens=1024, - n=1, - stop=None, - temperature=0.5, - ) - message = completions.choices[0].text - answer = message.strip().split("\nAnswer:")[-1].strip() - return answer - - -tools = [ - Tool( - name = "wikipedia", - func= ask_wiki, - description=" Useful when you need factual information about topics, concepts, events and people. on wikipedia" +def askme(question): + + def ask_wiki(question): + wiki = WikipediaAPIWrapper() + + + result = wiki.run(question) + template = """ + Please write a concise and to the point summary, make it easy to understand, include all important points from the following text: + {result} + Write only those points whhich are related to the {question} + """ + prompt = PromptTemplate(input_variables=["result","question"], template=template) + summary_prompt = prompt.format(result=result,question=question) + summary = llm(summary_prompt) + + return summary + + + def ask_REPL(question): + python_repl = PythonREPL() + result = python_repl.run(question) + return result + + + def ask_web(question): + search = DuckDuckGoSearchRun() + result = search.run(question) + + template = """ + Please write a concise and to the point summary, easy to understand, include all important points from the following text: + {result} + Write only those points which are related to the {question} + Do NOT write unrelated points + """ + prompt = PromptTemplate(input_variables=["result","question"], template=template) + summary_prompt = prompt.format(result=result,question=question) + summary = llm(summary_prompt) + + return summary + + + # print(ask_web(question)) + + def ask_openai(question): + prompt = f"Question: {question}\n" "Answer:" + completions = openai.Completion.create( + engine=MODEL_ENGINE, + prompt=prompt, + max_tokens=1024, + n=1, + stop=None, + temperature=0.5, + ) + message = completions.choices[0].text + answer = message.strip().split("\nAnswer:")[-1].strip() + return answer + + + tools = [ + Tool( + name = "wikipedia", + func= ask_wiki, + description=" Useful when you need factual information about topics, concepts, events and people. on wikipedia" + ) + ] + + repl_tool = Tool( + name='Python REPL', + func= ask_REPL, + description="useful for when you need to use python to answer a question. You should input python code" + ) + + duckduckgo_tool = Tool( + name='Web Search', + func= ask_web, + description="Useful for when you need to do a search on the internet to find information that another tool can't find, or when you need up-to-date factual information that goes beyond your existing knowledge. be specific with your input." ) -] -repl_tool = Tool( - name='Python REPL', - func= ask_REPL, - description="useful for when you need to use python to answer a question. You should input python code" + openai_tool = Tool( + name="openai search", + func= ask_openai, + description='Useful when you need accurate and insightful answers to complex questions that go beyond factual information.' ) -duckduckgo_tool = Tool( - name='Web Search', - func= ask_web, - description="Useful for when you need to do a search on the internet to find information that another tool can't find, or when you need up-to-date factual information that goes beyond your existing knowledge. be specific with your input." -) - -openai_tool = Tool( - name="openai search", - func= ask_openai, - description='Useful when you need accurate and insightful answers to complex questions that go beyond factual information.' -) + tools.append(duckduckgo_tool) + tools.append(repl_tool) + tools.append(openai_tool) -tools.append(duckduckgo_tool) -tools.append(repl_tool) -tools.append(openai_tool) - -zero_shot_agent = initialize_agent( - agent="zero-shot-react-description", - tools=tools, - llm=llm, - verbose=True, - max_iterations=3, -) + zero_shot_agent = initialize_agent( + agent="zero-shot-react-description", + tools=tools, + llm=llm, + verbose=False, + max_iterations=3, + ) -answer = zero_shot_agent.run(question) + answer = zero_shot_agent.run(question) + return answer