web-search endpoint that you can use to search the web outside of a chat completion. See the web search API guide for more information.
This guide explains how to use the web search tool in a chat completion and provides an example of how to use it.
You need an API key to access the Writer API. Get an API key by following the steps in the API quickstart.We recommend setting the API key as an environment variable in a
.env file with the name WRITER_API_KEY.Tool structure
The web search tool allows you to search the web during a chat with a Palmyra model. To use the web search tool, add it to thetools array in your chat-completion endpoint request.
The web search tool object has the following structure:
| Parameter | Type | Description |
|---|---|---|
type | string | The type of tool, which is web_search for the web search tool |
function | object | An object containing the tool’s configuration |
function.include_domains | array | An array of domains to include in the search results |
function.exclude_domains | array | An array of domains to exclude from the search results |
You can only pass one prebuilt tool in the
tools array at a time. However, you can pass multiple custom tools in the same request.Prebuilt tools are:- Web search tool
- Knowledge Graph tool
- LLM tool
- Translation tool
- Vision tool
Response format
For non-streaming responses, the search results and answer are in thechoices[0].message.content field. For streaming responses, the search results and answer are in the choices[0].delta.content field.
The response also contains a web_search_data field that contains the following information:
| Parameter | Type | Description |
|---|---|---|
query | string | The search query that was submitted |
answer | string | Generated answer based on the search results |
sources | array | The search results found |
sources[].url | string | URL of the search result |
sources[].raw_content | string | Raw content from the source URL (if requested) |
Usage example
This example uses the web search tool to find current information about AI developments during a chat completion.Create a tools array containing a web search tool
First, create atools array that specifies the web search tool you want to use.
Send the request using chat completions
Add the tools array to the chat endpoint call along with your array of messages. Settingtool_choice to auto allows the model to choose when to use the web search tool, based on the message provided in the messages array.
The response contains the search results and generated answer in the choices[0].message.content field.
web_search_data field to see the search query, answer, and sources that the web search tool used.