Spaces:
Running
Running
File size: 2,273 Bytes
b8cdbfb f893655 b8cdbfb f893655 b8cdbfb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import os
from duckpy import Client
from langchain import PromptTemplate, OpenAI, LLMChain
from langchain.agents import Tool
from langchain.base_language import BaseLanguageModel
MAX_SEARCH_RESULTS = 20 # Number of search results to observe at a time
search_description = """ Useful for when you need to ask with search. Use direct language and be
EXPLICIT in what you want to search.
## Examples of incorrect use
1. Action: Search
Action Input: "[name of bagel shop] menu"
The Action Input cannot be None or empty.
"""
notepad_description = """ Useful for when you need to note-down specific
information for later reference. Please provide full information you want to
note-down in the Action Input and all future prompts will remember it.
This is the mandatory tool after using the search tool.
Using Notepad does not always lead to a final answer.
## Exampels of using notepad tool
Action: Notepad
Action Input: the information you want to note-down
"""
async def ddg(query: str):
if query is None or query.lower().strip().strip('"') == "none" or query.lower().strip().strip('"') == "null":
x = "The action input field is empty. Please provide a search query."
return [x]
else:
client = Client()
return client.search(query)[:MAX_SEARCH_RESULTS]
async def notepad(x: str) -> str:
return f"{[x]}"
search_tool = Tool(name="Search",
func=lambda x: x,
coroutine=ddg,
description=search_description)
note_tool = Tool(name="Notepad",
func=lambda x: x,
coroutine=notepad,
description=notepad_description)
def rewrite_search_query(q: str, search_history, llm: BaseLanguageModel) -> str:
history_string = '\n'.join(search_history)
template ="""We are using the Search tool.
# Previous queries:
{history_string}. \n\n Rewrite query {action_input} to be
different from the previous ones."""
prompt = PromptTemplate(template=template,
input_variables=["action_input", "history_string"])
llm_chain = LLMChain(prompt=prompt, llm=llm)
return llm_chain.predict(action_input=q, history_string=history_string)
|