<!-- Source: https://docs.squirro.com/en/latest/technical/api/squirro_client/sc-api-genai.html -->
# GenAI service API

The class below is a mixin class which encapsulates the functionality of the GenAI service API. It is **not** a comprehensive list of all the methods available in the GenAI service API, as the methods may vary depending on a specific deployment configuration.

The source of truth for the GenAI service API should be the API documentation available under the `/docs` endpoint of the GenAI service. In a traditional deployment, the URL would be `https://<squirro-cluster>/service/genai/docs`.

All the methods of these classes are made available in the [`SquirroClient`](squirroclient-class.md#squirro_client.SquirroClient) class.

## Example Usage

> **Attention**
>
> To interact with the `genai` service via `SquirroClient`, ensure the service is accessible from the host where the `SquirroClient` is running. This is usually done by adjusting the Nginx configuration to allow external access to the `/service/genai` endpoint.

The following example demonstrates how to interact with the GenAI service API to get the answer to a simple prompt using the SquirroClient.

```python
from squirro_client import SquirroClient

# Set these variables accordingly:
REFRESH_TOKEN = "v4.public.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
CLUSTER="https://my-squirro-cluster.squirro.cloud" # Ensure `CLUSTER` **does not** have a trailing slash
GENAI_API_URL = CLUSTER + "/service/genai" # `GENAI_API_URL` may be different depending on where GenAI is deployed
PROJECT_ID = "your-project-id"
OPENAI_API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

client = SquirroClient(None, None, cluster=CLUSTER, genai_api_url=GENAI_API_URL)
client.authenticate(refresh_token=REFRESH_TOKEN)

json = {
    "instruction": "Tell me about France.",
    "runtime_config": {
        "llm_api_key": OPENAI_API_KEY, # may be required if the `genai` service has not been provided with one, **even** if the platform has one
        "squirro_refresh_token":  REFRESH_TOKEN, # currently, we need to provide the Squirro refresh token to allow the GenAI service to access project data. In the future, it'll be inferred from the access token created by the SquirroClient
    }
}

res = client.genai_request(
        "post",
        f"/v0/projects/{PROJECT_ID}/streaming_chat",
        json=json,
    )

for event in res.iter_lines():
    print(event)
```

For more complex usage, which includes retrieving documents from Squirro to provide context to the LLM, we can use agents:

```python
agents = client.get_agents(PROJECT_ID)

# The "All Data" agent is available in every project by default and attempts to retrieve documents related to the prompt from all data sources connected to the project
agent = next((a for a in agents if a["name"] == "All Data"), None)
agent_id = agent["id"]

json = {
    "instruction": "Who was Albert Einstein?",
    "agent_id": agent_id,
    "runtime_config": {
        "llm_api_key": OPENAI_API_KEY,
        "squirro_refresh_token":  REFRESH_TOKEN,
    }
}

res = client.genai_request(
        "post",
        f"/v0/projects/{PROJECT_ID}/streaming_chat",
        json=json,
    )

for event in res.iter_lines():
    print(event)
```

### Classes
