diff --git a/app/backend/approaches/readdecomposeask.py b/app/backend/approaches/readdecomposeask.py index 8dc728f074..da7a49ab6a 100644 --- a/app/backend/approaches/readdecomposeask.py +++ b/app/backend/approaches/readdecomposeask.py @@ -1,10 +1,11 @@ import openai +import re from approaches.approach import Approach from azure.search.documents import SearchClient from azure.search.documents.models import QueryType from langchain.llms.openai import AzureOpenAI from langchain.prompts import PromptTemplate, BasePromptTemplate -from langchain.callbacks.base import CallbackManager +from langchain.callbacks.manager import CallbackManager from langchain.agents import Tool, AgentExecutor from langchain.agents.react.base import ReActDocstoreAgent from langchainadapters import HtmlCallbackHandler @@ -69,8 +70,8 @@ def run(self, q: str, overrides: dict) -> any: llm = AzureOpenAI(deployment_name=self.openai_deployment, temperature=overrides.get("temperature") or 0.3, openai_api_key=openai.api_key) tools = [ - Tool(name="Search", func=lambda q: self.search(q, overrides)), - Tool(name="Lookup", func=self.lookup) + Tool(name="Search", func=lambda q: self.search(q, overrides), description="useful for when you need to ask with search", callbacks=cb_manager), + Tool(name="Lookup", func=self.lookup, description="useful for when you need to ask with lookup", callbacks=cb_manager) ] # Like results above, not great to keep this as a global, will interfere with interleaving @@ -83,8 +84,9 @@ def run(self, q: str, overrides: dict) -> any: chain = AgentExecutor.from_agent_and_tools(agent, tools, verbose=True, callback_manager=cb_manager) result = chain.run(q) - # Fix up references to they look like what the frontend expects ([] instead of ()), need a better citation format since parentheses are so common - result = result.replace("(", "[").replace(")", "]") + # Replace substrings of the form with [file.ext] so that the frontend can render them as links, match them with a regex to avoid + # generalizing too much and disrupt HTML snippets if present + result = re.sub(r"<([a-zA-Z0-9_ \-\.]+)>", r"[\1]", result) return {"data_points": self.results or [], "answer": result, "thoughts": cb_handler.get_and_reset_log()} @@ -97,111 +99,112 @@ def create_prompt(cls, tools: List[Tool]) -> BasePromptTemplate: EXAMPLES = [ """Question: What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into? -Thought 1: I need to search Colorado orogeny, find the area that the eastern sector +Thought: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado orogeny extends into, then find the elevation range of the area. -Action 1: Search[Colorado orogeny] -Observation 1: [info1.pdf] The Colorado orogeny was an episode of mountain building (an orogeny) in +Action: Search[Colorado orogeny] +Observation: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas. -Thought 2: It does not mention the eastern sector. So I need to look up eastern +Thought: It does not mention the eastern sector. So I need to look up eastern sector. -Action 2: Lookup[eastern sector] -Observation 2: [info2.txt] (Result 1 / 1) The eastern sector extends into the High Plains and is called +Action: Lookup[eastern sector] +Observation: (Result 1 / 1) The eastern sector extends into the High Plains and is called the Central Plains orogeny. -Thought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I +Thought: The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range. -Action 3: Search[High Plains] -Observation 3: [some_file.pdf] High Plains refers to one of two distinct land regions -Thought 4: I need to instead search High Plains (United States). -Action 4: Search[High Plains (United States)] -Observation 4: [filea.pdf] The High Plains are a subregion of the Great Plains. [another-ref.docx] From east to west, the +Action: Search[High Plains] +Observation: High Plains refers to one of two distinct land regions +Thought: I need to instead search High Plains (United States). +Action: Search[High Plains (United States)] +Observation: The High Plains are a subregion of the Great Plains. From east to west, the High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130 m). -Thought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer +Thought: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft. -Action 5: Finish[1,800 to 7,000 ft (filea.pdf) ]""", +Action: Finish[1,800 to 7,000 ft ]""", """Question: Musician and satirist Allie Goertz wrote a song about the "The Simpsons" character Milhouse, who Matt Groening named after who? -Thought 1: The question simplifies to "The Simpsons" character Milhouse is named after +Thought: The question simplifies to "The Simpsons" character Milhouse is named after who. I only need to search Milhouse and find who it is named after. -Action 1: Search[Milhouse] -Observation 1: [info7.pdf] Milhouse Mussolini Van Houten is a recurring character in the Fox animated +Action: Search[Milhouse] +Observation: Milhouse Mussolini Van Houten is a recurring character in the Fox animated television series The Simpsons voiced by Pamela Hayden and created by Matt Groening. -Thought 2: The paragraph does not tell who Milhouse is named after, maybe I can look up +Thought: The paragraph does not tell who Milhouse is named after, maybe I can look up "named after". -Action 2: Lookup[named after] -Observation 2: [historyref2.txt] (Result 1 / 1) Milhouse was named after U.S. president Richard Nixon, whose +Action: Lookup[named after] +Observation: (Result 1 / 1) Milhouse was named after U.S. president Richard Nixon, whose middle name was Milhous. -Thought 3: Milhouse was named after U.S. president Richard Nixon, so the answer is +Thought: Milhouse was named after U.S. president Richard Nixon, so the answer is Richard Nixon. -Action 3: Finish[Richard Nixon (historyref2.txt) ]""", +Action: Finish[Richard Nixon ]""", """Question: Which documentary is about Finnish rock groups, Adam Clayton Powell or The Saimaa Gesture? -Thought 1: I need to search Adam Clayton Powell and The Saimaa Gesture, and find which +Thought: I need to search Adam Clayton Powell and The Saimaa Gesture, and find which documentary is about Finnish rock groups. -Action 1: Search[Adam Clayton Powell] -Observation 1: [info9.pdf] Could not find [Adam Clayton Powell]. Similar: ['Adam Clayton Powell +Action: Search[Adam Clayton Powell] +Observation: Could not find [Adam Clayton Powell]. Similar: ['Adam Clayton Powell III', 'Seventh Avenue (Manhattan)', 'Adam Clayton Powell Jr. State Office Building', 'Isabel Washington Powell', 'Adam Powell', vAdam Clayton Powell (film)', 'Giancarlo Esposito']. -Thought 2: To find the documentary, I can search Adam Clayton Powell (film). -Action 2: Search[Adam Clayton Powell (film)] -Observation 2: [data123.txt] Adam Clayton Powell is a 1989 American documentary film directed by +Thought: To find the documentary, I can search Adam Clayton Powell (film). +Action: Search[Adam Clayton Powell (film)] +Observation: Adam Clayton Powell is a 1989 American documentary film directed by Richard Kilberg. The film is about the rise and fall of influential African-American politician Adam Clayton Powell Jr.[3][4] It was later aired as part of the PBS series The American Experience. -Thought 3: Adam Clayton Powell (film) is a documentary about an African-American +Thought: Adam Clayton Powell (film) is a documentary about an African-American politician, not Finnish rock groups. So the documentary about Finnish rock groups must instead be The Saimaa Gesture. -Action 3: Finish[The Saimaa Gesture (data123.txt) ]""", +Action: Finish[The Saimaa Gesture ]""", """Question: What profession does Nicholas Ray and Elia Kazan have in common? -Thought 1: I need to search Nicholas Ray and Elia Kazan, find their professions, then +Thought: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common. -Action 1: Search[Nicholas Ray] -Observation 1: [files-987.png] Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, +Action: Search[Nicholas Ray] +Observation: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause. -Thought 2: Professions of Nicholas Ray are director, screenwriter, and actor. I need +Thought: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions. -Action 2: Search[Elia Kazan] -Observation 2: [files-654.txt] Elia Kazan was an American film and theatre director, producer, screenwriter +Action: Search[Elia Kazan] +Observation: Elia Kazan was an American film and theatre director, producer, screenwriter and actor. -Thought 3: Professions of Elia Kazan are director, producer, screenwriter, and actor. +Thought: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor. -Action 3: Finish[director, screenwriter, actor (files-987.png)(files-654.txt) ]""", +Action: Finish[director, screenwriter, actor ]""", """Question: Which magazine was started first Arthur's Magazine or First for Women? -Thought 1: I need to search Arthur's Magazine and First for Women, and find which was +Thought: I need to search Arthur's Magazine and First for Women, and find which was started first. -Action 1: Search[Arthur's Magazine] -Observation 1: [magazines-1850.pdf] Arthur's Magazine (1844-1846) was an American literary periodical published +Action: Search[Arthur's Magazine] +Observation: Arthur's Magazine (1844-1846) was an American literary periodical published in Philadelphia in the 19th century. -Thought 2: Arthur's Magazine was started in 1844. I need to search First for Women +Thought: Arthur's Magazine was started in 1844. I need to search First for Women next. -Action 2: Search[First for Women] -Observation 2: [magazines-1900.pdf] First for Women is a woman's magazine published by Bauer Media Group in the +Action: Search[First for Women] +Observation: First for Women is a woman's magazine published by Bauer Media Group in the USA.[1] The magazine was started in 1989. -Thought 3: First for Women was started in 1989. 1844 (Arthur's Magazine) < 1989 (First +Thought: First for Women was started in 1989. 1844 (Arthur's Magazine) < 1989 (First for Women), so Arthur's Magazine was started first. -Action 3: Finish[Arthur's Magazine (magazines-1850.pdf)(magazines-1900.pdf) ]""", +Action: Finish[Arthur's Magazine ]""", """Question: Were Pavel Urysohn and Leonid Levin known for the same type of work? -Thought 1: I need to search Pavel Urysohn and Leonid Levin, find their types of work, +Thought: I need to search Pavel Urysohn and Leonid Levin, find their types of work, then find if they are the same. -Action 1: Search[Pavel Urysohn] -Observation 1: [info4444.pdf] Pavel Samuilovich Urysohn (February 3, 1898 - August 17, 1924) was a Soviet +Action: Search[Pavel Urysohn] +Observation: Pavel Samuilovich Urysohn (February 3, 1898 - August 17, 1924) was a Soviet mathematician who is best known for his contributions in dimension theory. -Thought 2: Pavel Urysohn is a mathematician. I need to search Leonid Levin next and +Thought: Pavel Urysohn is a mathematician. I need to search Leonid Levin next and find its type of work. -Action 2: Search[Leonid Levin] -Observation 2: [datapoints_aaa.txt] Leonid Anatolievich Levin is a Soviet-American mathematician and computer +Action: Search[Leonid Levin] +Observation: Leonid Anatolievich Levin is a Soviet-American mathematician and computer scientist. -Thought 3: Leonid Levin is a mathematician and computer scientist. So Pavel Urysohn +Thought: Leonid Levin is a mathematician and computer scientist. So Pavel Urysohn and Leonid Levin have the same type of work. -Action 3: Finish[yes (info4444.pdf)(datapoints_aaa.txt) ]""", +Action: Finish[yes ]""", ] SUFFIX = """\nQuestion: {input} {agent_scratchpad}""" PREFIX = "Answer questions as shown in the following examples, by splitting the question into individual search or lookup actions to find facts until you can answer the question. " \ -"Observations are prefixed by their source name in square brackets, source names MUST be included with the actions in the answers." \ -"Only answer the questions using the information from observations, do not speculate." +"Observations are prefixed by their source name in angled brackets, source names MUST be included with the actions in the answers." \ +"All questions must be answered from the results from search or look up actions, only facts resulting from those can be used in an answer. " +"Answer questions as truthfully as possible, and ONLY answer the questions using the information from observations, do not speculate or your own knowledge." diff --git a/app/backend/approaches/readretrieveread.py b/app/backend/approaches/readretrieveread.py index 4ba69f0107..5d58aa94f1 100644 --- a/app/backend/approaches/readretrieveread.py +++ b/app/backend/approaches/readretrieveread.py @@ -3,7 +3,7 @@ from azure.search.documents import SearchClient from azure.search.documents.models import QueryType from langchain.llms.openai import AzureOpenAI -from langchain.callbacks.base import CallbackManager +from langchain.callbacks.manager import CallbackManager, Callbacks from langchain.chains import LLMChain from langchain.agents import Tool, ZeroShotAgent, AgentExecutor from langchain.llms.openai import AzureOpenAI @@ -77,8 +77,11 @@ def run(self, q: str, overrides: dict) -> any: cb_handler = HtmlCallbackHandler() cb_manager = CallbackManager(handlers=[cb_handler]) - acs_tool = Tool(name = "CognitiveSearch", func = lambda q: self.retrieve(q, overrides), description = self.CognitiveSearchToolDescription) - employee_tool = EmployeeInfoTool("Employee1") + acs_tool = Tool(name="CognitiveSearch", + func=lambda q: self.retrieve(q, overrides), + description=self.CognitiveSearchToolDescription, + callbacks=cb_manager) + employee_tool = EmployeeInfoTool("Employee1", callbacks=cb_manager) tools = [acs_tool, employee_tool] prompt = ZeroShotAgent.create_prompt( @@ -103,8 +106,12 @@ def run(self, q: str, overrides: dict) -> any: class EmployeeInfoTool(CsvLookupTool): employee_name: str = "" - def __init__(self, employee_name: str): - super().__init__(filename = "data/employeeinfo.csv", key_field = "name", name = "Employee", description = "useful for answering questions about the employee, their benefits and other personal information") + def __init__(self, employee_name: str, callbacks: Callbacks = None): + super().__init__(filename="data/employeeinfo.csv", + key_field="name", + name="Employee", + description="useful for answering questions about the employee, their benefits and other personal information", + callbacks=callbacks) self.func = self.employee_info self.employee_name = employee_name diff --git a/app/backend/langchainadapters.py b/app/backend/langchainadapters.py index 74c1a81e72..c057d1b21a 100644 --- a/app/backend/langchainadapters.py +++ b/app/backend/langchainadapters.py @@ -44,12 +44,12 @@ def on_chain_error(self, error: Exception, **kwargs: Any) -> None: def on_tool_start( self, serialized: Dict[str, Any], - action: AgentAction, + input_str: str, color: Optional[str] = None, **kwargs: Any, ) -> None: """Print out the log in specified color.""" - self.html += f"{ch(action.log)}
" + pass def on_tool_end( self, @@ -69,12 +69,18 @@ def on_text( self, text: str, color: Optional[str] = None, - end: str = "", **kwargs: Optional[str], ) -> None: """Run when agent ends.""" self.html += f"{ch(text)}
" + def on_agent_action( + self, + action: AgentAction, + color: Optional[str] = None, + **kwargs: Any) -> Any: + self.html += f"{ch(action.log)}
" + def on_agent_finish( self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any ) -> None: diff --git a/app/backend/lookuptool.py b/app/backend/lookuptool.py index fc169a331c..c15da53f5f 100644 --- a/app/backend/lookuptool.py +++ b/app/backend/lookuptool.py @@ -1,12 +1,16 @@ from os import path import csv from langchain.agents import Tool +from langchain.callbacks.manager import Callbacks from typing import Optional class CsvLookupTool(Tool): - def __init__(self, filename: path, key_field: str, name: str = "lookup", description: str = "useful to look up details given an input key as opposite to searching data with an unstructured question"): - super().__init__(name, self.lookup, description) - self.data = {} + data: dict[str, str] = {} + + def __init__(self, filename: path, key_field: str, name: str = "lookup", + description: str = "useful to look up details given an input key as opposite to searching data with an unstructured question", + callbacks: Callbacks = None): + super().__init__(name, self.lookup, description, callbacks=callbacks) with open(filename, newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: diff --git a/app/backend/requirements.txt b/app/backend/requirements.txt index 49508fa697..6119dccf84 100644 --- a/app/backend/requirements.txt +++ b/app/backend/requirements.txt @@ -1,6 +1,6 @@ azure-identity==1.13.0b3 Flask==2.2.2 -langchain==0.0.78 +langchain==0.0.187 openai==0.26.4 azure-search-documents==11.4.0b3 azure-storage-blob==12.14.1 diff --git a/notebooks/read-decompose-ask.ipynb b/notebooks/read-decompose-ask.ipynb index 6c3c6428bd..f45a131eaa 100644 --- a/notebooks/read-decompose-ask.ipynb +++ b/notebooks/read-decompose-ask.ipynb @@ -61,119 +61,119 @@ "EXAMPLES = [\n", " \"\"\"Question: What is the elevation range for the area that the eastern sector of the\n", "Colorado orogeny extends into?\n", - "Thought 1: I need to search Colorado orogeny, find the area that the eastern sector\n", + "Thought: I need to search Colorado orogeny, find the area that the eastern sector\n", "of the Colorado orogeny extends into, then find the elevation range of the\n", "area.\n", - "Action 1: Search[Colorado orogeny]\n", - "Observation 1: [info1.pdf] The Colorado orogeny was an episode of mountain building (an orogeny) in\n", + "Action: Search[Colorado orogeny]\n", + "Observation: The Colorado orogeny was an episode of mountain building (an orogeny) in\n", "Colorado and surrounding areas.\n", - "Thought 2: It does not mention the eastern sector. So I need to look up eastern\n", + "Thought: It does not mention the eastern sector. So I need to look up eastern\n", "sector.\n", - "Action 2: Lookup[eastern sector]\n", - "Observation 2: [info2.txt] (Result 1 / 1) The eastern sector extends into the High Plains and is called\n", + "Action: Lookup[eastern sector]\n", + "Observation: (Result 1 / 1) The eastern sector extends into the High Plains and is called\n", "the Central Plains orogeny.\n", - "Thought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I\n", + "Thought: The eastern sector of Colorado orogeny extends into the High Plains. So I\n", "need to search High Plains and find its elevation range.\n", - "Action 3: Search[High Plains]\n", - "Observation 3: [some_file.pdf] High Plains refers to one of two distinct land regions\n", - "Thought 4: I need to instead search High Plains (United States).\n", - "Action 4: Search[High Plains (United States)]\n", - "Observation 4: [filea.pdf] The High Plains are a subregion of the Great Plains. [another-ref.docx] From east to west, the\n", + "Action: Search[High Plains]\n", + "Observation: High Plains refers to one of two distinct land regions\n", + "Thought: I need to instead search High Plains (United States).\n", + "Action: Search[High Plains (United States)]\n", + "Observation: The High Plains are a subregion of the Great Plains. From east to west, the\n", "High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130\n", "m).\n", - "Thought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer\n", + "Thought: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer\n", "is 1,800 to 7,000 ft.\n", - "Action 5: Finish[1,800 to 7,000 ft [filea.pdf]]\"\"\",\n", + "Action: Finish[1,800 to 7,000 ft ]\"\"\",\n", " \"\"\"Question: Musician and satirist Allie Goertz wrote a song about the \"The Simpsons\"\n", "character Milhouse, who Matt Groening named after who?\n", - "Thought 1: The question simplifies to \"The Simpsons\" character Milhouse is named after\n", + "Thought: The question simplifies to \"The Simpsons\" character Milhouse is named after\n", "who. I only need to search Milhouse and find who it is named after.\n", - "Action 1: Search[Milhouse]\n", - "Observation 1: [info7.pdf] Milhouse Mussolini Van Houten is a recurring character in the Fox animated\n", + "Action: Search[Milhouse]\n", + "Observation: Milhouse Mussolini Van Houten is a recurring character in the Fox animated\n", "television series The Simpsons voiced by Pamela Hayden and created by Matt\n", "Groening.\n", - "Thought 2: The paragraph does not tell who Milhouse is named after, maybe I can look up\n", + "Thought: The paragraph does not tell who Milhouse is named after, maybe I can look up\n", "\"named after\".\n", - "Action 2: Lookup[named after]\n", - "Observation 2: [historyref2.txt] (Result 1 / 1) Milhouse was named after U.S. president Richard Nixon, whose\n", + "Action: Lookup[named after]\n", + "Observation: (Result 1 / 1) Milhouse was named after U.S. president Richard Nixon, whose\n", "middle name was Milhous.\n", - "Thought 3: Milhouse was named after U.S. president Richard Nixon, so the answer is\n", + "Thought: Milhouse was named after U.S. president Richard Nixon, so the answer is\n", "Richard Nixon.\n", - "Action 3: Finish[Richard Nixon [historyref2.txt]]\"\"\",\n", + "Action: Finish[Richard Nixon ]\"\"\",\n", " \"\"\"Question: Which documentary is about Finnish rock groups, Adam Clayton Powell or The\n", "Saimaa Gesture?\n", - "Thought 1: I need to search Adam Clayton Powell and The Saimaa Gesture, and find which\n", + "Thought: I need to search Adam Clayton Powell and The Saimaa Gesture, and find which\n", "documentary is about Finnish rock groups.\n", - "Action 1: Search[Adam Clayton Powell]\n", - "Observation 1: [info9.pdf] Could not find [Adam Clayton Powell]. Similar: ['Adam Clayton Powell\n", + "Action: Search[Adam Clayton Powell]\n", + "Observation: Could not find [Adam Clayton Powell]. Similar: ['Adam Clayton Powell\n", "III', 'Seventh Avenue (Manhattan)', 'Adam Clayton Powell Jr. State Office\n", "Building', 'Isabel Washington Powell', 'Adam Powell', vAdam Clayton Powell\n", "(film)', 'Giancarlo Esposito'].\n", - "Thought 2: To find the documentary, I can search Adam Clayton Powell (film).\n", - "Action 2: Search[Adam Clayton Powell (film)]\n", - "Observation 2: [data123.txt] Adam Clayton Powell is a 1989 American documentary film directed by\n", + "Thought: To find the documentary, I can search Adam Clayton Powell (film).\n", + "Action: Search[Adam Clayton Powell (film)]\n", + "Observation: Adam Clayton Powell is a 1989 American documentary film directed by\n", "Richard Kilberg. The film is about the rise and fall of influential\n", "African-American politician Adam Clayton Powell Jr.[3][4] It was later aired\n", "as part of the PBS series The American Experience.\n", - "Thought 3: Adam Clayton Powell (film) is a documentary about an African-American\n", + "Thought: Adam Clayton Powell (film) is a documentary about an African-American\n", "politician, not Finnish rock groups. So the documentary about Finnish rock\n", "groups must instead be The Saimaa Gesture.\n", - "Action 3: Finish[The Saimaa Gesture [data123.txt]]\"\"\",\n", + "Action: Finish[The Saimaa Gesture ]\"\"\",\n", " \"\"\"Question: What profession does Nicholas Ray and Elia Kazan have in common?\n", - "Thought 1: I need to search Nicholas Ray and Elia Kazan, find their professions, then\n", + "Thought: I need to search Nicholas Ray and Elia Kazan, find their professions, then\n", "find the profession they have in common.\n", - "Action 1: Search[Nicholas Ray]\n", - "Observation 1: [files-987.png] Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16,\n", + "Action: Search[Nicholas Ray]\n", + "Observation: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16,\n", "1979) was an American film director, screenwriter, and actor best known for\n", "the 1955 film Rebel Without a Cause.\n", - "Thought 2: Professions of Nicholas Ray are director, screenwriter, and actor. I need\n", + "Thought: Professions of Nicholas Ray are director, screenwriter, and actor. I need\n", "to search Elia Kazan next and find his professions.\n", - "Action 2: Search[Elia Kazan]\n", - "Observation 2: [files-654.txt] Elia Kazan was an American film and theatre director, producer, screenwriter\n", + "Action: Search[Elia Kazan]\n", + "Observation: Elia Kazan was an American film and theatre director, producer, screenwriter\n", "and actor.\n", - "Thought 3: Professions of Elia Kazan are director, producer, screenwriter, and actor.\n", + "Thought: Professions of Elia Kazan are director, producer, screenwriter, and actor.\n", "So profession Nicholas Ray and Elia Kazan have in common is director,\n", "screenwriter, and actor.\n", - "Action 3: Finish[director, screenwriter, actor [files-987.png][files-654.txt]]\"\"\",\n", + "Action: Finish[director, screenwriter, actor ]\"\"\",\n", " \"\"\"Question: Which magazine was started first Arthur's Magazine or First for Women?\n", - "Thought 1: I need to search Arthur's Magazine and First for Women, and find which was\n", + "Thought: I need to search Arthur's Magazine and First for Women, and find which was\n", "started first.\n", - "Action 1: Search[Arthur's Magazine]\n", - "Observation 1: [magazines-1850.pdf] Arthur's Magazine (1844-1846) was an American literary periodical published\n", + "Action: Search[Arthur's Magazine]\n", + "Observation: Arthur's Magazine (1844-1846) was an American literary periodical published\n", "in Philadelphia in the 19th century.\n", - "Thought 2: Arthur's Magazine was started in 1844. I need to search First for Women\n", + "Thought: Arthur's Magazine was started in 1844. I need to search First for Women\n", "next.\n", - "Action 2: Search[First for Women]\n", - "Observation 2: [magazines-1900.pdf] First for Women is a woman's magazine published by Bauer Media Group in the\n", + "Action: Search[First for Women]\n", + "Observation: First for Women is a woman's magazine published by Bauer Media Group in the\n", "USA.[1] The magazine was started in 1989.\n", - "Thought 3: First for Women was started in 1989. 1844 (Arthur's Magazine) < 1989 (First\n", + "Thought: First for Women was started in 1989. 1844 (Arthur's Magazine) < 1989 (First\n", "for Women), so Arthur's Magazine was started first.\n", - "Action 3: Finish[Arthur's Magazine [magazines-1850.pdf][magazines-1900.pdf]]\"\"\",\n", + "Action: Finish[Arthur's Magazine ]\"\"\",\n", " \"\"\"Question: Were Pavel Urysohn and Leonid Levin known for the same type of work?\n", - "Thought 1: I need to search Pavel Urysohn and Leonid Levin, find their types of work,\n", + "Thought: I need to search Pavel Urysohn and Leonid Levin, find their types of work,\n", "then find if they are the same.\n", - "Action 1: Search[Pavel Urysohn]\n", - "Observation 1: [info4444.pdf] Pavel Samuilovich Urysohn (February 3, 1898 - August 17, 1924) was a Soviet\n", + "Action: Search[Pavel Urysohn]\n", + "Observation: Pavel Samuilovich Urysohn (February 3, 1898 - August 17, 1924) was a Soviet\n", "mathematician who is best known for his contributions in dimension theory.\n", - "Thought 2: Pavel Urysohn is a mathematician. I need to search Leonid Levin next and\n", + "Thought: Pavel Urysohn is a mathematician. I need to search Leonid Levin next and\n", "find its type of work.\n", - "Action 2: Search[Leonid Levin]\n", - "Observation 2: [datapoints_aaa.txt] Leonid Anatolievich Levin is a Soviet-American mathematician and computer\n", + "Action: Search[Leonid Levin]\n", + "Observation: Leonid Anatolievich Levin is a Soviet-American mathematician and computer\n", "scientist.\n", - "Thought 3: Leonid Levin is a mathematician and computer scientist. So Pavel Urysohn\n", + "Thought: Leonid Levin is a mathematician and computer scientist. So Pavel Urysohn\n", "and Leonid Levin have the same type of work.\n", - "Action 3: Finish[yes [info4444.pdf][datapoints_aaa.txt]]\"\"\",\n", + "Action: Finish[yes ]\"\"\",\n", "]\n", "SUFFIX = \"\"\"\\nQuestion: {input}\n", "{agent_scratchpad}\"\"\"\n", "PREFIX = \"Answer questions as shown in the following examples, by splitting the question into individual search or lookup actions to find facts until you can answer the question. \" \\\n", - "\"Observations are prefixed by their source name in square brackets, source names MUST be included with the actions in the answers.\" \\\n", + "\"Observations are prefixed by their source name in angled brackets, source names MUST be included with the actions in the answers.\" \\\n", "\"All questions must be answered from the results from search or look up actions, only facts resulting from those can be used in an answer. \"\n", "\"Answer questions as truthfully as possible, and ONLY answer the questions using the information from observations, do not speculate or your own knowledge.\"\n", "\n", "prompt = PromptTemplate.from_examples(\n", " EXAMPLES, SUFFIX, [\"input\", \"agent_scratchpad\"], PREFIX\n", - ")\n" + ")" ] }, { @@ -197,7 +197,7 @@ " query_language=\"en-us\", \n", " query_speller=\"lexicon\", \n", " semantic_configuration_name=\"default\")\n", - " return \"\\n\".join([f\"[{doc[KB_FIELDS_SOURCEPAGE]}] \" + (doc[KB_FIELDS_CONTENT][:500]).replace(\"\\n\", \"\").replace(\"\\r\", \"\") for doc in r])\n", + " return \"\\n\".join([f\"<{doc[KB_FIELDS_SOURCEPAGE]}> \" + (doc[KB_FIELDS_CONTENT][:500]).replace(\"\\n\", \"\").replace(\"\\r\", \"\") for doc in r])\n", "\n", "def lookup(terms: str):\n", " print (\"\\nlooking up: \" + terms)\n", @@ -221,8 +221,8 @@ "\n", "llm = AzureOpenAI(deployment_name=AZURE_OPENAI_GPT_DEPLOYMENT, temperature=0.3, openai_api_key=openai.api_key)\n", "tools = [\n", - " Tool(name=\"Search\", func=search),\n", - " Tool(name=\"Lookup\", func=lookup)\n", + " Tool(name=\"Search\", func=search, description=\"useful for when you need to ask with search\"),\n", + " Tool(name=\"Lookup\", func=lookup, description=\"useful for when you need to ask with lookup\" )\n", "]\n", "\n", "class ReAct(ReActDocstoreAgent):\n", @@ -263,7 +263,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.10" + "version": "3.10.11" }, "orig_nbformat": 4, "vscode": { diff --git a/notebooks/requirements.txt b/notebooks/requirements.txt index 8577f257f3..17c7421e4b 100644 --- a/notebooks/requirements.txt +++ b/notebooks/requirements.txt @@ -1,4 +1,4 @@ azure-identity==1.12.0 -langchain==0.0.78 +langchain==0.0.187 openai==0.26.4 azure-search-documents==11.4.0b3