forked from langroid/langroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/add sciphi search rag (langroid#350)
* Add sciphi provider * undo readme change * add search rag tool * cleanup * fix flag * add agent-search dependency, pydantic->1.10.13, lints * chat-search small mods * sciphi test -> tests/extras; make agent-search optional --------- Co-authored-by: Owen <ocolegro@gmail.com>
- Loading branch information
1 parent
b00ba1d
commit 151a11f
Showing
3 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
A tool which returns a Search RAG response from the SciPhi API. | ||
their titles, links, summaries. Since the tool is stateless (i.e. does not need | ||
access to agent state), it can be enabled for any agent, without having to define a | ||
special method inside the agent: `agent.enable_message(SciPhiSearchRAGTool)` | ||
Example return output appears as follows below: | ||
<-- Query --> | ||
``` | ||
Find 3 results on the internet about the LK-99 superconducting material. | ||
`` | ||
<-- Response (compressed for this example)--> | ||
``` | ||
[ result1 ] | ||
[ result2 ] | ||
[ result3 ] | ||
``` | ||
NOTE: Using this tool requires getting an API key from sciphi.ai. | ||
Setup is as simple as shown below | ||
# Get a free API key at https://www.sciphi.ai/account | ||
# export SCIPHI_API_KEY=$MY_SCIPHI_API_KEY before running the agent | ||
# OR add SCIPHI_API_KEY=$MY_SCIPHI_API_KEY to your .env file | ||
This tool requires installing langroid with the `sciphi` extra, e.g. | ||
`pip install langroid[sciphi]` or `poetry add langroid[sciphi]` | ||
(it installs the `agent-search` package from pypi). | ||
For more information, please refer to the official docs: | ||
https://agent-search.readthedocs.io/en/latest/ | ||
""" | ||
|
||
try: | ||
from agent_search import SciPhi | ||
except ImportError: | ||
raise ImportError( | ||
"You are attempting to use the `agent-search` library;" | ||
"To use it, please install langroid with the `sciphi` extra, e.g. " | ||
"`pip install langroid[sciphi]` or `poetry add langroid[sciphi]` " | ||
"(it installs the `agent-search` package from pypi)." | ||
) | ||
|
||
from langroid.agent.tool_message import ToolMessage | ||
|
||
|
||
class SciPhiSearchRAGTool(ToolMessage): | ||
request: str = "web_search_rag" | ||
purpose: str = """ | ||
To search the web with provider <search_provider> and | ||
return a response summary with llm model <llm_model> the given <query>. | ||
""" | ||
query: str | ||
search_provider: str = "bing" # bing or agent-search | ||
include_related_queries: bool = True | ||
llm_model: str = "SciPhi/Sensei-7B-V1" | ||
recursive_mode: bool = True | ||
|
||
def handle(self) -> str: | ||
rag_response = SciPhi().get_search_rag_response( | ||
query=self.query, | ||
search_provider=self.search_provider, | ||
llm_model=self.llm_model, | ||
) | ||
result = rag_response["response"] | ||
if self.include_related_queries: | ||
result = ( | ||
f"### RAG Response:\n{result}\n\n" | ||
+ "### Related Queries:\n" | ||
+ "\n".join(rag_response["related_queries"]) | ||
) | ||
return result # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters