Add your description here
Project description
Xecai - Minimalistic, provider-agnostic, AI python library
Develop code for different AI services and providers, change very few lines. Easily customise the behaviour so that it fits your requirements.
Examples
Chat interface
from xecai.chat.implementations.openai.openai_chat import OpenAIChat
from xecai.models import Message, MessageType
messages = [Message(content="what model are you?", message_type=MessageType.USER)]
system_prompt = "you are a helpful bot"
model = "gpt-4o"
chat = OpenAIChat()
chat.check_model(model)
response = chat.invoke(model, system_prompt, messages)
print(response)
for chat_response in chat.stream(model, system_prompt, messages):
if chat_response.text:
print(chat_response.text, end="", flush=True)
Agent interface
import asyncio
from typing import Any
from xecai.agents.implementations.openai.openai_agent import OpenAIAgent
from xecai.agents.agent_interface import WebSearchTool, tool
from xecai.models import Message, MessageType
@tool
def divide(args: dict[str, Any]) -> Any:
"""Divide number 'a' by number 'b'. Requires 'a' and 'b'."""
a = float(args.get("a", 0))
b = float(args.get("b", 1))
if b == 0:
result = "Error: Division by zero"
else:
result = a / b
print(f"\n [Tool used: divide(a={a}, b={b}) -> {result}]")
return result
async test():
system_prompt = (
"You are a helpful search assistant. "
"Always use the tools provided to get accurate information, "
"otherwise say you can't do it. Don't say approximately, use the exact tool result value."
)
model = "gpt-4o"
agent = OpenAIAgent()
scenarios = [
("what global news happened today?", [WebSearchTool]), # Web search example
("divide 234324.23423 by 342.124324234", [divide]) # Custom function example
]
for user_query, tools in scenarios:
messages = [Message(content=user_query, message_type=MessageType.USER)]
response = await agent.async_run(
model_name=model,
system_prompt=system_prompt,
messages=messages,
tools=tools,
)
print(response)
asyncio.run(test())
VectorDB interface
from xecai.vector_db.implementations.postgresql.postgresql_vector_db import PostgreSQLVectorDB
from xecai.embeddings.implementations.openai.openai_embedding import OpenAIEmbedding
from xecai.models import SearchType
vector_db = PostgreSQLVectorDB(
embedding_interface=OpenAIEmbedding(), embedding_model="text-embedding-3-small"
)
chunks = vector_db.sync_retrieve(
query="this is an example query",
k=3,
search_type=SearchType.HYBRID,
)
print(chunks)
Memory interface
from xecai.memory.implementations.postgresql.postgresql_memory import PostgreSQLMemory
from xecai.models import Conversation, Message, MessageType
memory = PostgreSQLMemory()
conversation = memory.sync_get_conversation("example_conversation_id") or Conversation()
print(conversation)
conversation.messages.append(Message(message_type=MessageType.USER, content="example query"))
memory.sync_save_conversation(conversation)
Embedding interface
from xecai.embeddings.implementations.openai.openai_embedding import OpenAIEmbedding
embedding = OpenAIEmbedding()
vector = embedding.sync_get_embeddings("This is a test document.", "text-embedding-3-small")
print(len(vector))
Reranker interface
from xecai.reranker.implementations.aws.aws_reranker import AWSReranker
from xecai.models import Chunk
reranker = AWSReranker()
chunks = [
Chunk(content="A document about cats.", document="doc1", origin="web", fragment=0),
Chunk(content="A document about dogs.", document="doc2", origin="web", fragment=0)
]
reranked_chunks = reranker.sync_rerank("tell me about cats", chunks, k=1)
print(reranked_chunks)
Typical rag workflow
Diagram
graph TD
%% Refined Palette with Intense Cherry and Ink Black
%% Outlines perfectly hidden by matching stroke to fill color
classDef input fill:#0074D9,stroke:#0074D9,color:#FFFFFF,font-weight:bold,rx:8,ry:8;
classDef process fill:#003366,stroke:#003366,color:#FFFFFF,rx:8,ry:8;
classDef condition fill:#FF851B,stroke:#FF851B,color:#FFFFFF,font-weight:bold,rx:8,ry:8;
%% Lighter Grey for the output node
classDef output fill:#F8FAFC,stroke:#F8FAFC,color:#0B0F19,font-weight:bold,rx:8,ry:8;
%% Intense Cherry
classDef condense fill:#BA0C2F,stroke:#BA0C2F,color:#FFFFFF,rx:8,ry:8;
%% Ink Black
classDef retrieve fill:#1B1F29,stroke:#0B0F19,color:#FFFFFF,rx:8,ry:8;
%% Clean, subtle slate-gray connecting lines
linkStyle default stroke:#A0AAB2,stroke-width:2px,fill:none;
%% Nodes
Query([Receive User Query]):::input
FetchHistory[Fetch Conversation History]:::process
CheckHistory{History Exists?}:::condition
CondenseQuery[Condense Query with Context]:::condense
RetrieveChunks[(Retrieve Context Chunks)]:::retrieve
LLMResponse([Generate LLM Response]):::output
%% Flow
Query --> FetchHistory
FetchHistory --> CheckHistory
CheckHistory -- "Yes" --> CondenseQuery
CheckHistory -- "No" --> RetrieveChunks
CondenseQuery --> RetrieveChunks
RetrieveChunks --> LLMResponse
You can find an example of the typical RAG implemented with FastAPI on examples/simple_rag.py.
Differences with projects that have a similar objective
| Library | Notes |
|---|---|
| JustLLMs | Requests are made directly with http. More LLMs supported and many more features (agents, tools, etc.). I personally prefer using the official SDKs. |
| LiteLLM & Agents SDK + LiteLLM | Local proxy router where you send the requests to be translated, adds complexity to the project and another element to the infrastructure. |
| LangChain | The most popular one. Bloated in some features, complex implementations that make it difficult to change its behaviours. |
| OpenRouter | All requests are sent to a 3rd party + additional charges. |
Notes
- I will implement Azure OpenAI's chat implementation if this gets enough traction (I don't want to loose my free Azure credits unless it is worth it).
- While using Chat objects, retries are left to the user to put and customise (check out
examples/retry_example.py), as it adds a lot of complexity to have a default retry logic but also let the user modify it.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xecai-0.3.0.tar.gz.
File metadata
- Download URL: xecai-0.3.0.tar.gz
- Upload date:
- Size: 30.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c302352969366cbda18cfd7559d91c9abc8ae94b260178299039edcb4a308d39
|
|
| MD5 |
386a4bc22404549c75967b3014821a3a
|
|
| BLAKE2b-256 |
229fa883edb38e914620440f24816bcb7254cb7dea510cc743014d352dceaebf
|
Provenance
The following attestation bundles were made for xecai-0.3.0.tar.gz:
Publisher:
publish.yml on AdrianVispalia/xecai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xecai-0.3.0.tar.gz -
Subject digest:
c302352969366cbda18cfd7559d91c9abc8ae94b260178299039edcb4a308d39 - Sigstore transparency entry: 1154778160
- Sigstore integration time:
-
Permalink:
AdrianVispalia/xecai@dbdd820fbd803261f692f53ab2831bc52adcb11e -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/AdrianVispalia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dbdd820fbd803261f692f53ab2831bc52adcb11e -
Trigger Event:
release
-
Statement type:
File details
Details for the file xecai-0.3.0-py3-none-any.whl.
File metadata
- Download URL: xecai-0.3.0-py3-none-any.whl
- Upload date:
- Size: 43.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a5b9b14832fa625f32a0d2fa83e9c5a3542afde2e8a3e5d4b746a03e098cfaa
|
|
| MD5 |
911a4b5d21de67e7ee736bf2f6678c2b
|
|
| BLAKE2b-256 |
64b23110f31d7636c68692bd60c27635dd49aafff15cd0b0c8500d5337c39fb6
|
Provenance
The following attestation bundles were made for xecai-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on AdrianVispalia/xecai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xecai-0.3.0-py3-none-any.whl -
Subject digest:
8a5b9b14832fa625f32a0d2fa83e9c5a3542afde2e8a3e5d4b746a03e098cfaa - Sigstore transparency entry: 1154778166
- Sigstore integration time:
-
Permalink:
AdrianVispalia/xecai@dbdd820fbd803261f692f53ab2831bc52adcb11e -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/AdrianVispalia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dbdd820fbd803261f692f53ab2831bc52adcb11e -
Trigger Event:
release
-
Statement type: