PerplexitySearchTool - AG2
PerplexitySearchTool
autogen.tools.experimental.perplexity.perplexity_search.PerplexitySearchTool
PerplexitySearchTool(model='sonar', api_key=None, max_tokens=1000, search_domain_filter=None)
Bases: Tool
Tool for interacting with the Perplexity AI search API.
This tool uses the Perplexity API to perform web search, news search, and conversational search, returning concise and precise responses.
| ATTRIBUTE | DESCRIPTION |
|---|---|
url |
API endpoint URL. TYPE: str |
model |
Name of the model to be used. TYPE: str |
api_key |
API key for authenticating with the Perplexity API. TYPE: str |
max_tokens |
Maximum tokens allowed for the API response. TYPE: int |
search_domain_filters |
Optional list of domain filters for the search. TYPE: Optional[list[str]] |
Initializes a new instance of the PerplexitySearchTool.
| PARAMETER | DESCRIPTION |
|---|---|
model |
The model to use. Defaults to "sonar". TYPE: strDEFAULT:'sonar' |
api_key |
API key for authentication. TYPE: Optional[str]DEFAULT:None |
max_tokens |
Maximum number of tokens for the response. Defaults to 1000. TYPE: intDEFAULT:1000 |
search_domain_filter |
List of domain filters to restrict search. TYPE: Optional[list[str]]DEFAULT:None |
| RAISES | DESCRIPTION |
|---|---|
ValueError |
If the API key is missing, the model is empty, max_tokens is not positive, or if search_domain_filter is not a list when provided. |
Source code in autogen/tools/experimental/perplexity/perplexity_search.py
<br>117<br>118<br>119<br>120<br>121<br>122<br>123<br>124<br>125<br>126<br>127<br>128<br>129<br>130<br>131<br>132<br>133<br>134<br>135<br>136<br>137<br>138<br>139<br>140<br>141<br>142<br>143<br>144<br>145<br>146<br>147<br>148<br>149<br> |
<br>def __init__(<br> self,<br> model: str = "sonar",<br> api_key: Optional[str] = None,<br> max_tokens: int = 1000,<br> search_domain_filter: Optional[list[str]] = None,<br>):<br> """<br> Initializes a new instance of the PerplexitySearchTool.<br> Args:<br> model (str, optional): The model to use. Defaults to "sonar".<br> api_key (Optional[str], optional): API key for authentication.<br> max_tokens (int, optional): Maximum number of tokens for the response. Defaults to 1000.<br> search_domain_filter (Optional[list[str]], optional): List of domain filters to restrict search.<br> Raises:<br> ValueError: If the API key is missing, the model is empty, max_tokens is not positive,<br> or if search_domain_filter is not a list when provided.<br> """<br> self.api_key = api_key or os.getenv("PERPLEXITY_API_KEY")<br> self._validate_tool_config(model, self.api_key, max_tokens, search_domain_filter)<br> self.url = "https://api.perplexity.ai/chat/completions"<br> self.model = model<br> self.api_key = api_key<br> self.max_tokens = max_tokens<br> self.search_domain_filters = search_domain_filter<br> super().__init__(<br> name="perplexity-search",<br> description="Perplexity AI search tool for web search, news search, and conversational search "<br> "for finding answers to everyday questions, conducting in-depth research and analysis.",<br> func_or_tool=self.search,<br> )<br> |
url instance-attribute
url = 'https://api.perplexity.ai/chat/completions'
model instance-attribute
model = model
api_key instance-attribute
api_key = api_key
max_tokens instance-attribute
max_tokens = max_tokens
search_domain_filters instance-attribute
search_domain_filters = search_domain_filter
name property
name
description property
description
func property
func
tool_schema property
tool_schema
Get the schema for the tool.
This is the preferred way of handling function calls with OpenAI and compatible frameworks.
function_schema property
function_schema
Get the schema for the function.
This is the old way of handling function calls with OpenAI and compatible frameworks. It is provided for backward compatibility.
realtime_tool_schema property
realtime_tool_schema
Get the schema for the tool.
This is the preferred way of handling function calls with OpenAI and compatible frameworks.
search
search(query)
Perform a search query using the Perplexity AI API.
Constructs the payload, executes the query, and parses the response to return a concise search result along with any provided citations.
| PARAMETER | DESCRIPTION |
|---|---|
query |
The search query. TYPE: str |
| RETURNS | DESCRIPTION |
|---|---|
SearchResponse |
A model containing the search result content and citations. TYPE: SearchResponse |
| RAISES | DESCRIPTION |
|---|---|
ValueError |
If the search query is invalid. |
RuntimeError |
If there is an error during the search process. |
Source code in autogen/tools/experimental/perplexity/perplexity_search.py
<br>193<br>194<br>195<br>196<br>197<br>198<br>199<br>200<br>201<br>202<br>203<br>204<br>205<br>206<br>207<br>208<br>209<br>210<br>211<br>212<br>213<br>214<br>215<br>216<br>217<br>218<br>219<br>220<br>221<br>222<br>223<br>224<br>225<br> |
<br>def search(self, query: Annotated[str, "The search query."]) -> SearchResponse:<br> """<br> Perform a search query using the Perplexity AI API.<br> Constructs the payload, executes the query, and parses the response to return<br> a concise search result along with any provided citations.<br> Args:<br> query (str): The search query.<br> Returns:<br> SearchResponse: A model containing the search result content and citations.<br> Raises:<br> ValueError: If the search query is invalid.<br> RuntimeError: If there is an error during the search process.<br> """<br> payload = {<br> "model": self.model,<br> "messages": [{"role": "system", "content": "Be precise and concise."}, {"role": "user", "content": query}],<br> "max_tokens": self.max_tokens,<br> "search_domain_filter": self.search_domain_filters,<br> "web_search_options": {"search_context_size": "high"},<br> }<br> try:<br> perplexity_response = self._execute_query(payload)<br> content = perplexity_response.choices[0].message.content<br> citations = perplexity_response.citations<br> return SearchResponse(content=content, citations=citations, error=None)<br> except Exception as e:<br> return SearchResponse(<br> content=None, citations=None, error=f"PerplexitySearchTool failed to search. Error: {e}"<br> )<br> |
register_for_llm
register_for_llm(agent)
Registers the tool for use with a ConversableAgent's language model (LLM).
This method registers the tool so that it can be invoked by the agent during interactions with the language model.
| PARAMETER | DESCRIPTION |
|---|---|
agent |
The agent to which the tool will be registered. TYPE: ConversableAgent |
Source code in autogen/tools/tool.py
<br>72<br>73<br>74<br>75<br>76<br>77<br>78<br>79<br>80<br>81<br> |
<br>def register_for_llm(self, agent: "ConversableAgent") -> None:<br> """Registers the tool for use with a ConversableAgent's language model (LLM).<br> This method registers the tool so that it can be invoked by the agent during<br> interactions with the language model.<br> Args:<br> agent (ConversableAgent): The agent to which the tool will be registered.<br> """<br> agent.register_for_llm()(self)<br> |
register_for_execution
register_for_execution(agent)
Registers the tool for direct execution by a ConversableAgent.
This method registers the tool so that it can be executed by the agent, typically outside of the context of an LLM interaction.
Source code in autogen/tools/tool.py
<br>83<br>84<br>85<br>86<br>87<br>88<br>89<br>90<br>91<br>92<br> |
<br>def register_for_execution(self, agent: "ConversableAgent") -> None:<br> """Registers the tool for direct execution by a ConversableAgent.<br> This method registers the tool so that it can be executed by the agent,<br> typically outside of the context of an LLM interaction.<br> Args:<br> agent (ConversableAgent): The agent to which the tool will be registered.<br> """<br> agent.register_for_execution()(self)<br> |
register_tool
register_tool(agent)
Register a tool to be both proposed and executed by an agent.
Equivalent to calling both register_for_llm and register_for_execution with the same agent.
Note: This will not make the agent recommend and execute the call in the one step. If the agent recommends the tool, it will need to be the next agent to speak in order to execute the tool.
Source code in autogen/tools/tool.py
<br> 94<br> 95<br> 96<br> 97<br> 98<br> 99<br>100<br>101<br>102<br>103<br>104<br>105<br>106<br> |
<br>def register_tool(self, agent: "ConversableAgent") -> None:<br> """Register a tool to be both proposed and executed by an agent.<br> Equivalent to calling both `register_for_llm` and `register_for_execution` with the same agent.<br> Note: This will not make the agent recommend and execute the call in the one step. If the agent<br> recommends the tool, it will need to be the next agent to speak in order to execute the tool.<br> Args:<br> agent (ConversableAgent): The agent to which the tool will be registered.<br> """<br> self.register_for_llm(agent)<br> self.register_for_execution(agent)<br> |