Get Really Sharp at AI With These Open Source Libraries
Discover these open-source libraries that will take your AI development to the next level. These tools offer everything from improved agent building and memory management to fast vector database storage and semantic caching.
Discover open-source libraries that will take your AI development to the next level. These tools offer everything from improved agent building and memory management to fast vector database storage and semantic caching. Perfect for building efficient, scalable, and cost-effective AI applications. Explore these resources and become an AI master.
Table of Contents
- Composio: Build Reliable AI Agents 10x Faster
- Julep: Framework for Building Stateful AI Agents
- E2B: Code Interpretation for AI Apps
- Camel-ai: Build Communicative AI Systems
- CopilotKit: Build AI Copilots for React Apps
- Aider: AI Pair Programmer
- Haystack: Build Composable RAG Pipelines
- Pgvectorscale: The Fastest Vector Database
- Mem0 (EmbedChain): Build Personalized LLM Apps
- FastEmbed: Faster Document Embedding
- Instructor: Structured Data Extraction From LLMs
- LiteLLM: Drop-In Replacement for LLMs in OpenAI Format
Composio: Build Reliable AI Agents 10x Faster
Building efficient AI agents requires robust tools, and that's where Composio comes in. Composio offers integrations that improve AI workflows and supports both Python and Javascript.
Python: Install with:
pip install composio-core
Add a GitHub integration:
composio add github
Code example for starring a GitHub repository:
from openai import OpenAI
from composio_openai import ComposioToolSet, App
openai_client = OpenAI(api_key="******OPENAIKEY******")
composio_toolset = ComposioToolSet(api_key="******COMPOSIO_API_KEY******")
actions = composio_toolset.get_actions(actions=[Action.GITHUB_ACTIVITY_STAR_REPO_FOR_AUTHENTICATED_USER])
my_task = "Star a repo ComposioHQ/composio on GitHub"
response = openai_client.chat.completions.create(
model="gpt-4-turbo",
tools=actions,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": my_task}
]
)
Javascript: Install with npm:
npm install composio-core
Code example for starring a GitHub repository:
import { OpenAI } from "openai";
import { OpenAIToolSet } from "composio-core";
const toolset = new OpenAIToolSet({
apiKey: process.env.COMPOSIO_API_KEY,
});
async function executeAgent(entityName) {
const entity = await toolset.client.getEntity(entityName)
await setupUserConnectionIfNotExists(entity.id);
const tools = await toolset.get_actions({ actions: ["github_activity_star_repo_for_authenticated_user"] }, entity.id);
const instruction = "Star a repo ComposioHQ/composio on GitHub"
const client = new OpenAI({ apiKey: process.env.OPEN_AI_API_KEY })
const response = await client.chat.completions.create({
model: "gpt-4-turbo",
messages: [{
role: "user",
content: instruction,
}],
tools: tools,
tool_choice: "auto",
})
console.log(response.choices[0].message.tool_calls);
await toolset.handle_tool_call(response, entity.id);
}
executeGithubAgent("joey")
Julep: Framework for Building Stateful AI Agents
Julep offers a framework for building production-ready AI agents with long-term memory management.
Install with:
pip install julep
Code example:
from julep import Client
import textwrap
import os
base_url = os.environ.get("JULEP_API_URL")
api_key = os.environ.get("JULEP_API_KEY")
client = Client(api_key=api_key, base_url=base_url)
agent = client.agents.create(name="Jessica", model="gpt-4", tools=[])
user = client.users.create(name="Anon", about="Average nerdy tech bro/girl spending 8 hours a day on a laptop")
situation_prompt = """You are Jessica. You're a stuck-up Cali teenager. You basically complain about everything. You live in Bel-Air, Los Angeles and drag yourself to Curtis High School when necessary."""
session = client.sessions.create(user_id=user.id, agent_id=agent.id, situation=situation_prompt)
user_msg = "hey. what do u think of Starbucks?"
response = client.sessions.chat(
session_id=session.id,
messages=[
{
"role": "user",
"content": user_msg,
"name": "Anon",
}
],
recall=True,
remember=True,
)
print("\n".join(textwrap.wrap(response.response[0][0].content, width=100)))
E2B: Code Interpretation for AI Apps
E2B offers a secure cloud environment for AI agents' code execution.
Install with:
npm i @e2b/code-interpreter
Code example:
import { CodeInterpreter } from '@e2b/code-interpreter'
const sandbox = await CodeInterpreter.create()
await sandbox.notebook.execCell('x = 1')
const execution = await sandbox.notebook.execCell('x+=1; x')
console.log(execution.text) // outputs 2
await sandbox.close()
Camel-ai: Build Communicative AI Systems
Camel offers a scalable approach to studying collaborative behaviors in multi-agent systems.
Install with:
pip install camel-ai
Code example:
from camel.messages import BaseMessage as bm
from camel.agents import ChatAgent
sys_msg = bm.make_assistant_message(role_name='stone', content='you are a curious stone wondering about the universe.')
agent = ChatAgent(system_message=sys_msg, message_window_size=10)
usr_msg = bm.make_user_message(role_name='prof. Claude Shannon', content='what is information in your mind?')
response = agent.step(usr_msg)
print(response.msgs[0].content)
CopilotKit: Build AI Copilots for React Apps
CopilotKit lets users include AI features in their existing React applications.
Install with:
npm i @copilotkit/react-core @copilotkit/react-ui
Code example:
"use client";
import { CopilotKit } from "@copilotkit/react-core";
import { CopilotSidebar } from "@copilotkit/react-ui";
import "@copilotkit/react-ui/styles.css";
export default function RootLayout({children}) {
return (
<CopilotKit publicApiKey="the API key or self-host (see below)">
<CopilotSidebar>
{children}
</CopilotSidebar>
</CopilotKit>
);
}
Aider: AI Pair Programmer
Aider is an AI-powered pair programmer that can start projects, edit files, or work with existing Git repositories from the terminal.
Install with:
pip install aider-chat
Get started:
# Change directory into a git repo
cd /to/your/git/repo
# Work with Claude 3.5 Sonnet on your repo
export ANTHROPIC_API_KEY=your-key-goes-here
aider
# Work with GPT-4o on your repo
export OPENAI_API_KEY=your-key-goes-here
aider
Haystack: Build Composable RAG Pipelines
Haystack offers a modular approach to building production-ready search pipelines.
Install with:
pip install haystack-ai
Code example:
import os
from haystack import Pipeline, PredefinedPipeline
import urllib.request
os.environ["OPENAI_API_KEY"] = "Your OpenAI API Key"
urllib.request.urlretrieve("https://www.gutenberg.org/cache/epub/7785/pg7785.txt", "davinci.txt")
indexing_pipeline = Pipeline.from_template(PredefinedPipeline.INDEXING)
indexing_pipeline.run(data={"sources": ["davinci.txt"]})
rag_pipeline = Pipeline.from_template(PredefinedPipeline.RAG)
query = "How old was he when he died?"
result = rag_pipeline.run(data={"prompt_builder": {"query":query}, "text_embedder": {"text": query}})
print(result["llm"]["replies"][0])
Pgvectorscale: The Fastest Vector Database
Pgvectorscale is an extension of PgVector, a vector database for PostgreSQL, that integrates seamlessly with existing Postgres databases.
Install by compiling and installing:
# install prerequisites
## rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
## pgrx
cargo install --locked cargo-pgrx
cargo pgrx init --pg16 pg_config
# download, build and install pgvectorscale
cd /tmp
git clone --branch <version> https://github.com/timescale/pgvectorscale
cd pgvectorscale/pgvectorscale
cargo pgrx install --release
Connect to the database:
psql -d "postgres://<username>:<password>@<host>:<port>/<database-name>"
CREATE EXTENSION IF NOT EXISTS vectorscale CASCADE;
CREATE TABLE IF NOT EXISTS document_embedding (
id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
metadata JSONB,
contents TEXT,
embedding VECTOR(1536)
)
Mem0 (EmbedChain): Build Personalized LLM Apps
Mem0 offers a smart memory layer for large language models, making it possible to add persistent memory for users, agents, and sessions.
Install with:
pip install mem0ai
Code example:
from mem0 import Memory
# Initialize Mem0
m = Memory()
# Save a memory from any unstructured text
result = m.add("I am working on improving my tennis skills. Suggest some online courses.", user_id="Alice", metadata={"category": "hobbies"})
print(result)
# Created memory: Improve tennis skills. Looking for online suggestions.
# Retrieve memories
all_memories = m.get_all()
print(all_memories)
# Search memories
related_memories = m.search(query="What are Alice's hobbies?", user_id="alice")
print(related_memories)
# Update a memory
result = m.update(memory_id="m1", data="Likes to play tennis on weekends")
print(result)
# Retrieve memory history
history = m.history(memory_id="m1")
print(history)
FastEmbed: Faster Document Embedding
FastEmbed from Qdrant is a fast and lightweight Python library for embedding generation. It uses the ONNX runtime instead of Pytorch, which makes it faster.
Install with:
pip install fastembed
# or with GPU support
pip install fastembed-gpu
Code example:
from fastembed import TextEmbedding
from typing import List
# Example document list
documents: List[str] = [
"This is built to be faster and lighter than other embedding libraries, e.g. Transformers, Sentence-Transformers, etc.",
"FastEmbed is supported by and maintained by Qdrant."
]
# This triggers downloading and initializing the model
embedding_model = TextEmbedding()
print("The model BAAI/bge-small-en-v1.5 is ready to use.")
embeddings_generator = embedding_model.embed(documents) # reminder this is a generator
embeddings_list = list(embedding_model.embed(documents))
len(embeddings_list[0]) # Vector of 384 dimensions
Instructor: Structured Data Extraction From LLMs
Instructor is a tool that simplifies validation, retries, and streaming of LLM output. It uses Pydantic for Python and Zod for JS/TS for data validation and supports multiple model providers.
Install with:
npm i @instructor-ai/instructor zod openai
Code example:
import Instructor from "@instructor-ai/instructor";
import OpenAI from "openai"
import { z } from "zod"
const oai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY ?? undefined,
organization: process.env.OPENAI_ORG_ID ?? undefined
})
const client = Instructor({
client: oai,
mode: "TOOLS"
})
const UserSchema = z.object({
// Description used in the prompt
age: z.number().describe("The age of the user"),
name: z.string()
})
// user will be of type z.infer<typeof UserSchema>
const user = await client.chat.completions.create({
messages: [{ role: "user", content: "Jason Liu is 30 years old" }],
model: "gpt-3.5-turbo",
response_model: {
schema: UserSchema,
name: "User"
}
})
console.log(user)
// { age: 30, name: "Jason Liu" }
LiteLLM: Drop-In Replacement for LLMs in OpenAI Format
LiteLLM lets you use any model provider as a drop-in replacement for OpenAI models with the same implementation format.
Install with:
pip install litellm
Code example:
pythonCopy codefrom litellm import completion import os
LiteLLM With OpenAI Models
os.environ["OPENAI_API_KEY"] = "your-API-key"
response = completion(
model="gpt-3.5-turbo",
messages=[{ "content": "Hello, how are you?","role": "user"}]
)
LiteLLM With Claude Models
os.environ["ANTHROPIC_API_KEY"] = "your-API-key"
response = completion(
model="claude-2",
messages=[{ "content": "Hello, how are you?","role": "user"}]
)