LLM Clients
Clients for large language models, used by in-context learning.
OpenAIClient needs the optional llm dependencies: pip install 'termxtract[llm]'.
llm
THINKING = re.compile('<think>.*?(?:</think>|$)', re.DOTALL)
module-attribute
The reasoning that some models write before their answer.
LLMClient
Bases: Protocol
A large language model that answers a prompt, such as OpenAIClient.
complete(prompt)
Answer a prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt, sent as a single user message. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The model's answer. |
OpenAIClient(model, base_url=None, api_key=None, cache_dir=None, max_tokens=512, **options)
A model served by the OpenAI API or any server compatible with it, such as Ollama, vLLM or llama.cpp.
Answers are generated with temperature 0, so that they can be reproduced, and can be cached on disk, so that the same prompt is only sent once.
Connect to the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The name of the model, such as "gpt-4o-mini" or, for Ollama, "qwen2.5:7b". |
required |
base_url
|
Optional[str]
|
The URL of the API, such as
"http://localhost:11434/v1" for Ollama, or None for the
|
None
|
api_key
|
Optional[str]
|
The API key, or None for the |
None
|
cache_dir
|
Optional[Union[str, PathLike]]
|
A directory to keep each answer in, keyed by the model, the prompt and the options, or None to cache nothing. |
None
|
max_tokens
|
int
|
The maximum length of an answer, in tokens. |
512
|
**options
|
Any
|
Further parameters of the chat completions request, such as
|
{}
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If the optional 'llm' dependencies are missing. |
complete(prompt)
Answer a prompt, from the cache if it has been answered before.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt, sent as a single user message. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The model's answer, without any reasoning in |