Metadata-Version: 2.5
Name: llama-cpp-python-datacircle
Version: 0.1.0
Summary: llama-cpp-python tools for the Datacircle API: find the best GTM APIs
Project-URL: Homepage, https://datacircle.dev
Project-URL: Documentation, https://api.datacircle.dev/v1/openapi.json
Project-URL: Repository, https://github.com/waynehamadi/llama-cpp-python-datacircle
License-Expression: MIT
License-File: LICENSE
Keywords: api,api-pricing,b2b-data,datacircle,function-calling,gguf,gtm,llama-cpp-python,llama.cpp,local-llm,tool-calling,tools
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# llama-cpp-python-datacircle

Find the best GTM APIs. We benchmark all the GTM APIs and we put the results on our website.

Our website: https://datacircle.dev

This package gives [llama-cpp-python](https://github.com/abetlen/llama-cpp-python) four tools that call Datacircle's open API, https://api.datacircle.dev/v1. You pass the four tools to `Llama.create_chat_completion()` or to llama-cpp-python's OpenAI-compatible server. The package then runs each tool call the model returns. You need no key and no account.

| Tool | What the tool gives | Call |
| --- | --- | --- |
| `datacircle_list_apis` | Every GTM API on Datacircle, with the API's slug, number of plans, number of providers and best plan | `GET /apis` |
| `datacircle_best_plans` | One API's plans, sorted by score out of 100, best first. Each plan comes with a rank and a price per 1,000 calls | `GET /apis/{slug}/best` |
| `datacircle_search_plans` | Plans that match a provider or plan name, on all APIs or on one API. You can sort them by score, provider or price | `GET /plans` |
| `datacircle_provider` | The APIs a provider sells, and the provider's rank on each API | `GET /providers/{slug}` |

## Install

```bash
pip install llama-cpp-python-datacircle
```

llama-cpp-python-datacircle works with any version of `llama-cpp-python`, on Python 3.9 and later. The package does not import `llama_cpp`.

## Use the tools with the model's own chat template

Most GGUF models come with a chat template. llama-cpp-python uses that template when you give no `chat_format`. Some chat templates take tools. With one of those templates, the model writes each tool call in the text of the reply. `call_all()` reads those calls and gives you the tool messages to send back:

```python
from llama_cpp import Llama
from llama_cpp_datacircle import call_all, datacircle_tools

llm = Llama(model_path="qwen2.5-3b-instruct-q4_k_m.gguf", n_ctx=8192)
messages = [{"role": "user", "content": "Who are the 3 best providers for the email-finder API?"}]
reply = llm.create_chat_completion(messages=messages, tools=datacircle_tools())

while tool_messages := call_all(reply):
    messages.append(reply["choices"][0]["message"])
    messages.extend(tool_messages)
    reply = llm.create_chat_completion(messages=messages, tools=datacircle_tools())

print(reply["choices"][0]["message"]["content"])
```

On each new call, the model's chat template puts each tool message in the prompt. We tested this loop with Qwen 2.5 (3B) and Qwen 3 (1.7B). Qwen writes each call in a `<tool_call>` block. Llama 3.2 writes calls as JSON. `call_all()` reads both formats.

## Use the tools with chatml-function-calling

With `chat_format="chatml-function-calling"`, llama-cpp-python puts the model's tool calls in the reply's `tool_calls`. `call_all()` runs those calls like calls from any other chat format. In llama-cpp-python 0.3.38, chatml-function-calling leaves tool messages out of the prompt. So the model never reads the tool's answer. To let the model read tool answers, use the model's own chat template.

To make the model call one tool, name the tool in `tool_choice`. Naming a tool works with any chat format. llama-cpp-python then makes the model write only the tool's arguments. The call comes in `tool_calls`:

```python
reply = llm.create_chat_completion(
    messages=messages,
    tools=datacircle_tools(),
    tool_choice={"type": "function", "function": {"name": "datacircle_best_plans"}},
)
messages.append(reply["choices"][0]["message"])
messages.extend(call_all(reply))
```

## Use the Datacircle tools beside your own tools

You can put the Datacircle tools in the same list as yours: `tools=[*datacircle_tools(), my_tool]`.

`tool_calls()` gives every call in a reply, including calls to your own tools. `call()` runs one call. For your own tools, `call()` returns `None`. Your code then runs the tool itself:

```python
from llama_cpp_datacircle import call, tool_calls

calls = tool_calls(reply)
messages.append(reply["choices"][0]["message"])
for tool_call in calls:
    messages.append(call(tool_call) or my_run_tool(tool_call))
```

Each call is an OpenAI-format tool call with an `id`. Each tool message has `role: "tool"`, the call's id in `tool_call_id`, and the answer as JSON text in `content`.

To give the model only some of the tools, list their names: `datacircle_tools(["datacircle_best_plans"])`.

## Use the tools with the OpenAI-compatible server

Start llama-cpp-python's server, then call the server with the OpenAI client. `call_all()` reads the reply from the OpenAI client:

```bash
pip install 'llama-cpp-python[server]' openai
python -m llama_cpp.server --model qwen2.5-3b-instruct-q4_k_m.gguf --n_ctx 8192
```

```python
from openai import OpenAI
from llama_cpp_datacircle import call_all, datacircle_tools

client = OpenAI(base_url="http://localhost:8000/v1", api_key="none")
messages = [{"role": "user", "content": "Who are the 3 best providers for the email-finder API?"}]
reply = client.chat.completions.create(model="local", messages=messages, tools=datacircle_tools())

while tool_messages := call_all(reply):
    messages.append(reply.choices[0].message.model_dump(exclude_none=True))
    messages.extend(tool_messages)
    reply = client.chat.completions.create(model="local", messages=messages, tools=datacircle_tools())

print(reply.choices[0].message.content)
```

## Call a tool yourself

You can call each tool without a model. You get the JSON text of the tool's answer:

```python
import json

from llama_cpp_datacircle import run

answer = json.loads(run("datacircle_best_plans", {"slug": "email-finder", "limit": 3}))
print(answer["plans"])
```

## What the model reads

Each tool gives the model a JSON object as text. Each plan comes with a rank, provider, plan, score, price per 1,000 (`perThousand`, in dollars) and a page on datacircle.dev. That page lists every field.

You can give a provider's name in place of a slug. The tool turns `Bright Data` into `bright-data`.

The tool returns `{"error": "..."}` with the API's message when the API gives no data, such as for an unknown slug. The error for an unknown API slug lists every valid slug. The tool checks each number from the model first. For a number out of range, the tool returns `{"error": "..."}` and makes no API call. If the network fails, the tool returns `{"error": "..."}` with the network error's message. The model can then call the tool again.

## Network

Your model runs on your machine. Each tool call makes one HTTPS GET request to https://api.datacircle.dev/v1 and nothing else. The request holds only the tool's own arguments: no key and nothing from your chats. The API takes up to 60 calls a minute from one IP address. The API can ask the tool to wait. The tool then waits once, for a minute at most. Each call uses only Python's standard library. Nothing goes to disk.

## Links

- Datacircle: https://datacircle.dev
- The API: https://datacircle.dev/api
- The API's spec: https://api.datacircle.dev/v1/openapi.json
- Every field of a plan: https://datacircle.dev/fields

MIT license.
