Skip to content

Commit

Permalink
date info reinforcement in webagent tool
Browse files Browse the repository at this point in the history
  • Loading branch information
fatihozturkh2o committed Nov 5, 2024
1 parent 7c78134 commit cc279e5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
22 changes: 19 additions & 3 deletions openai_server/agent_tools/web_agent_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
API_KEY = os.getenv('H2OGPT_API_KEY')
API_BASE = os.getenv('H2OGPT_OPENAI_BASE_URL')
BING_API_KEY = os.getenv('BING_API_KEY')

# print(f"MODEL: {MODEL}, API_KEY: {API_KEY}, API_BASE: {API_BASE}, BING_API_KEY: {BING_API_KEY}")

class LLMCallbackHandler(BaseCallbackHandler):

Expand Down Expand Up @@ -75,6 +75,9 @@ class ImproveCode(BaseModel):
with open(f"{cwd}/openai_server/browser/prompts/improve_code.txt") as f:
IMPROVE_CODE_PROMPT_TEMPLATE = f.read()

with open(f"{cwd}/openai_server/browser/prompts/date_info.txt") as f:
DATE_INFO_PROMPT_TEMPLATE = f.read()

class WebAgent:
def __init__(self):
# TODO: is max_tokens ok?
Expand All @@ -85,7 +88,7 @@ def __init__(self):
self.tool_choice_output_parser = JsonOutputParser(pydantic_object=ToolChoice)
choose_tool_prompt = PromptTemplate(
template=CHOOSE_TOOL_PROMPT_TEMPLATE,
input_variables=['steps', 'question'],
input_variables=['steps', 'question', 'date_info'],
partial_variables={"format_instructions": self.tool_choice_output_parser.get_format_instructions()}
)
self.choose_tool_chain = choose_tool_prompt | self.llm | self.tool_choice_output_parser
Expand Down Expand Up @@ -210,12 +213,24 @@ def ask(self, raw_question: str, attachment_file_path: str = None) -> str:
question = raw_question
# pp(f"Question: {question}")

try:
date_info_prompt = PromptTemplate(
template=DATE_INFO_PROMPT_TEMPLATE,
input_variables=['question'],
)
date_info_fetcher = date_info_prompt | self.llm | StrOutputParser()
date_info = date_info_fetcher.invoke({'question': question})
print(f"Date Info: {date_info}")
except Exception as e:
print(f"Error: {e}")
date_info = None

for _ in range(20):
# TODO: pass has_error info to the choose_tool_chain
has_error = False
for _ in range(10):
try:
tool_choice = self.choose_tool_chain.invoke({'question': question, 'steps': '\n\n'.join(steps)})
tool_choice = self.choose_tool_chain.invoke({'question': question, 'steps': '\n\n'.join(steps), 'date_info': date_info})
# h2ogpt models may return with 'properties' key
if 'properties' in tool_choice:
tool_choice = tool_choice['properties']
Expand Down Expand Up @@ -259,6 +274,7 @@ def ask(self, raw_question: str, attachment_file_path: str = None) -> str:
if tool == 'None':
print(f"No tool chosen, break")
break
# TODO: Shouldnt populate agent history with the tool results?
if tool_result:
print(f"\n * Current tool result: {tool_result}")
step_note = self.summarize_tool_chain.invoke({'question': question, 'steps': '\n\n'.join(steps), 'tool_result': tool_result, 'tool': tool, 'args': args})
Expand Down
7 changes: 7 additions & 0 deletions openai_server/browser/prompts/choose_tool.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ I'll give you a question and a set of tools. Tell me which function you would us
{question}
```

# Date information for the current web search
```text
{date_info}
```

# Tools

## Browser
Expand Down Expand Up @@ -41,6 +46,8 @@ When the page is very long, content truncation may occur due to the limited disp
- If there is no mentions of specific date for website view histories, then always go with the up-to-date page. However, if there are mentions of certain dates, then make sure to visit relevant history pages before fetching information from web pages. If this is the case, look for tabs like 'History', 'View history' to locate old pages via find_on_page_ctrl_f.
- If you need to click tabs or buttons, you need to use 'href' information of these tabs or buttons from the page content. You need to visit pages with href relevant to the tabs. E.g. [Tab](/w/tab) then you need to visit the following page https://page.com/w/tab.
- If you need to search through large number of pages of a forum, or large number of history pages of a website, and you need to visit each of them to check some facts, use binary search to visit pages. E.g. always find the middle page first and visit it, if not find the desired answer then keep searching with the right half of the search space and find midle page of the right half space. If not, do the same for left half space, until you reach your desired answer.
- If you need to go through history of a Wikipedia page, you can use of the following example link: https://en.wikipedia.org/w/index.php?title=Turkey&action=history&date-range-to=2012-01-31&offset=&limit=50
- With the example link above, if you pass a date as 'date-range-to=2012-01-31', you'll get history results up-to&including the date 2012-01-31 and you'll get results for the last '50' history results. Based on user's request, you can change these values.

Based on the question and the step history, tell me which function you would use to solve the problem in next step.
If you don't need any function or the question is very easy to answer, function "None" is also an option.
Expand Down
10 changes: 10 additions & 0 deletions openai_server/browser/prompts/date_info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
User question:
```text
{question}
```

* Based on the user question, do you think the user is interested in finding web information from or up to a specific date? If so, please provide the date that the information should be from or up to.
* A specific date can be either very specific as hour/day/month/year level or a bit more vauge like just for certain year(s) or month(s).
* Especially web sites like Wikipedia, news sites, and other informational sites may have information that can change over time.
* So if the user is asking information from a specific time period, it is important to know that.
* If the user is not asking for information from a specific time period, you can type 'User is not interested in any date for the current web search, so it's safe to assume the user is interested in the most recent information.'.

0 comments on commit cc279e5

Please sign in to comment.