An AnythingLLM workspace answers questions with its own model settings and documents, and the chat box in the browser is only one way to reach it. The developer API's workspace chat endpoint takes a message for one workspace and returns that workspace's answer as JSON, so a help-desk bot or a scheduled report can ask the same workspace without opening the web interface.

The answer comes from the LLM provider and model that the workspace uses, together with any documents embedded in it, so the workspace needs a working chat model before the API can return anything useful. Requests name the workspace by its slug, the lowercase, hyphenated form of its name that AnythingLLM assigns when the workspace is created. Each request also chooses a chat mode. chat lets the model answer from its own general knowledge as well as the workspace documents, while query answers only from embedded documents and declines when nothing relevant is found. Leaving the mode out applies the mode saved in the workspace's chat settings.

A developer API key authorizes each request, and in AnythingLLM 1.19.0 that key is not limited to one workspace or to chat, so the key behind a chat integration can also read, change and delete every workspace on the instance. Requests without a valid key are turned away before the message reaches the model. The endpoint holds the HTTP connection open until the model has finished, which on a small CPU-only model takes a few seconds for a one-sentence answer and longer for long replies or larger models.

Steps to send a workspace chat through the AnythingLLM API:

  1. Copy an API key with Copy API Key on the Developer API page of AnythingLLM's settings.

    The wrench icon at the bottom of the workspace sidebar opens the settings, and Developer API sits under Tools. The button reads Copied once the key is on the clipboard. When the list is empty, Generate New API Key on the same page creates a key, and on a multi-user instance only an administrator can open this page.

  2. Store the copied key in a shell variable through a hidden read prompt.
    $ read -rs ANYTHINGLLM_API_KEY

    -s hides the pasted key and -r keeps it unchanged. Nothing appears while you paste, and Enter completes the input. The variable lasts only for this shell session and keeps the key out of the shell history.

  3. List the workspaces on the instance to find the slug of the workspace that should answer.
    $ curl -sS -H @- http://localhost:3001/api/v1/workspaces \
      <<< "Authorization: Bearer $ANYTHINGLLM_API_KEY" | python3 -m json.tool
    {
        "workspaces": [
            {
                "id": 2,
                "name": "Support Desk",
                "slug": "support-desk",
    ##### snipped #####

    -H @- reads the Authorization header from the here-string on the last line, which keeps the key out of the process list that other users on the host can read. python3 -m json.tool only indents the JSON for reading. A list that comes back with workspaces also shows that AnythingLLM accepted the key. An instance on another host uses its own address in place of localhost:3001. If the workspaces list is empty, the + button beside the workspace search box in the sidebar creates a workspace.

  4. Send a chat message to the workspace's chat endpoint with that slug.
    $ curl -sS -H @- -H 'Content-Type: application/json' \
      -d '{"message": "What does a support desk do? Answer in one sentence.", "mode": "chat"}' \
      http://localhost:3001/api/v1/workspace/support-desk/chat \
      <<< "Authorization: Bearer $ANYTHINGLLM_API_KEY" | python3 -m json.tool
    {
        "id": "3636d990-d948-47e9-a72f-00976aabc543",
        "type": "textResponse",
        "close": true,
        "error": null,
        "chatId": 3,
        "textResponse": "A support desk typically handles inquiries, assists with technical issues, provides information, and helps customers resolve problems or get assistance with products or services.",
        "sources": [],
        "metrics": {
    ##### snipped #####
            "model": "local-chat:latest",
            "provider": "OllamaAILLM",
    ##### snipped #####

    textResponse holds the workspace's answer, and "close": true with "error": null shows that the reply finished without a failure. model and provider under metrics name the model that answered, and the wording of the answer changes from run to run. A request without a valid key returns {"error":"No valid api key found."} with HTTP status 403, and a slug that matches no workspace returns status 400 with "error": "Workspace support-desks is not a valid workspace.". With "mode": "query" in a workspace that has no matching embedded document, textResponse holds the refusal "There is no relevant information in this workspace to answer your query." instead of an answer.

Share this guide