Web Search

Search and discuss with a LLM about web resources using Cosmos.

About Web Search

The Web Search service allows you to access a variety of websites with a single query, enabling a language model (LLM) to collect information from those sites and answer your questions. You can either choose specific websites beforehand or allow the research to span across any site. The LLM will then answer the question by providing a response and citing the sources it used.

The output_language for the response, which defaults to the language of the input question, can be set by providing an ISO 639 language code (such as fr for French, en for English, etc.), along with the desired_urls.


Step-by-step Tutorial

1. Prerequisites

Before you begin, ensure you have:

2. Setup Cosmos Python Client

Using Python Cosmos client you can perform the API requests in a convenient way.

2.1. Install Cosmos Python Client:

In order to install the Python client, you can add it to your project by doing:

poetry add delos-cosmos

Or install it in your virtual environment with:

pip install delos-cosmos

2.2. Authenticate Requests:

  • Initialize the client with your API key:
from cosmos import CosmosClient

cosmos_client = CosmosClient(apikey=your-cosmos-api-key)

2.3. Call API:

  • You can start invoking any Cosmos endpoints. For example, let's try the /health endpoint to check the validity of your API key and the availability of the client services:
response = cosmos_client.status_health_request()
print(response)

3. Perform a web search

The parameters for the web search request are:

ParameterDescriptionExample
textThe query text you want to search for"What is the capital of France?"
output_language (optional)The ISO 639-1 code for desired answer languageen-US for English
desired_urls (optional)List of websites to priorize in the search["wikipedia.fr"]

Here is an example of a Web Search request using Python client:


response = cosmos_client.web_search_request(text="Hello, World!", output_language="es")
print(response)

If your search request is successful, you will receive a response with details about the search results. For example:

{
  "request_id": "ab98c963-22b9-409e-8ec9-7188b87ae26f",
  "response_id": "d9a09ba2-d180-4e0c-b5d7-f19f01f0737f",
  "status_code": 200,
  "timestamp": "2024-10-16T07:25:45.128256Z",
  "status": "success",
  "message": "Search completed successfully",
  "data": {
    "reformulation": "What is the capital of France?",
    "urls": ["http://example.com/paris", "http://example.com/france"],
    "answer": "The capital of France is Paris."
  }
}

4. Handle Errors

If there is an issue with your request, CosmosPlatform will provide an error response. For example, if there is a required parameter missing in the request (text, output_language), you will receive a Bad Request error:

{
  "request_id": "af9c3e67-0a8b-4b62-a292-2c9371b3f022",
  "response_id": "2c78d8ac-b9b8-4e4b-ae56-37bd9b2a08f3",
  "status_code": 400,
  "timestamp": "2024-10-14T08:40:25Z",
  "status": "error",
  "message": "No text or output language provided.",
  "error": {
    "error_code": "400",
    "error_message": "No text provided for the search",
    "details": "The text parameter was missing."
  },
  "data": null
}