Skip to main content

A framework for building LLM based AI agents with llama.cpp.

Project description

llama-cpp-agent

llama-cpp-agent logo

Table of Contents

Introduction

The llama-cpp-agent framework is a tool designed to simplify interactions with Large Language Models (LLMs). It provides an interface for chatting with LLMs, executing function calls, generating structured output, performing retrieval augmented generation, and processing text using agentic chains with tools.

The framework uses guided sampling to constrain the model output to the user defined structures. This way also models not fine-tuned to do function calling and JSON output will be able to do it.

The framework is compatible with the llama.cpp server, llama-cpp-python and its server, and with TGI and vllm servers.

Key Features

  • Simple Chat Interface: Engage in seamless conversations with LLMs.
  • Structured Output: Generate structured output (objects) from LLMs.
  • Single and Parallel Function Calling: Execute functions using LLMs.
  • RAG - Retrieval Augmented Generation: Perform retrieval augmented generation with colbert reranking.
  • Agent Chains: Process text using agent chains with tools, supporting Conversational, Sequential, and Mapping Chains.
  • Guided Sampling: Allows most 7B LLMs to do function calling and structured output. Thanks to grammars and JSON schema generation for guided sampling.
  • Multiple Providers: Works with llama-cpp-python, llama.cpp server, TGI server and vllm server as provider!
  • Compatibility: Works with python functions, pydantic tools, llama-index tools, and OpenAI tool schemas.
  • Flexibility: Suitable for various applications, from casual chatting to specific function executions.

Installation

Install the llama-cpp-agent framework using pip:

pip install llama-cpp-agent

Documentation

You can find the latest documentation here!

Getting Started

You can find the get started guide here!

Discord Community

Join the Discord Community here

Usage Examples

The llama-cpp-agent framework provides a wide range of examples demonstrating its capabilities. Here are some key examples:

Simple Chat Example using llama.cpp server backend

This example demonstrates how to initiate a chat with an LLM model using the llama.cpp server backend. It supports llama-cpp-python Llama class instances, OpenAI endpoints with GBNF grammar support, and the llama.cpp backend server.

View Example

Parallel Function Calling Agent Example

This example showcases parallel function calling using the FunctionCallingAgent class. It demonstrates how to define and execute multiple functions concurrently.

View Example

Structured Output

This example illustrates how to generate structured output objects using the StructuredOutputAgent class. It shows how to create a dataset entry of a book from unstructured data.

View Example

RAG - Retrieval Augmented Generation

This example demonstrates Retrieval Augmented Generation (RAG) with colbert reranking. It requires installing the optional rag dependencies (ragatouille).

View Example

llama-index Tools Example

This example shows how to use llama-index tools and query engines with the FunctionCallingAgent class.

View Example

Sequential Chain Example

This example demonstrates how to create a complete product launch campaign using a sequential chain.

View Example

Mapping Chain Example

This example illustrates how to create a mapping chain to summarize multiple articles into a single summary.

View Example

Knowledge Graph Creation Example

This example, based on an example from the Instructor library for OpenAI, shows how to create a knowledge graph using the llama-cpp-agent framework.

View Example

Additional Information

Predefined Messages Formatter

The llama-cpp-agent framework provides predefined message formatters to format messages for the LLM model. The MessagesFormatterType enum defines the available formatters:

  • MessagesFormatterType.CHATML: Formats messages using the CHATML format.
  • MessagesFormatterType.MIXTRAL: Formats messages using the MIXTRAL format.
  • MessagesFormatterType.VICUNA: Formats messages using the VICUNA format.
  • MessagesFormatterType.LLAMA_2: Formats messages using the LLAMA 2 format.
  • MessagesFormatterType.SYNTHIA: Formats messages using the SYNTHIA format.
  • MessagesFormatterType.NEURAL_CHAT: Formats messages using the NEURAL CHAT format.
  • MessagesFormatterType.SOLAR: Formats messages using the SOLAR format.
  • MessagesFormatterType.OPEN_CHAT: Formats messages using the OPEN CHAT format.

Creating Custom Messages Formatter

You can create your own custom messages formatter by instantiating the MessagesFormatter class with the desired parameters:

from llama_cpp_agent.messages_formatter import MessagesFormatter

custom_formatter = MessagesFormatter(
    PRE_PROMPT="",
    SYS_PROMPT_START="<|system|>",
    SYS_PROMPT_END="<|endsystem|>",
    USER_PROMPT_START="<|user|>",
    USER_PROMPT_END="<|enduser|>",
    ASSISTANT_PROMPT_START="<|assistant|>",
    ASSISTANT_PROMPT_END="<|endassistant|>",
    INCLUDE_SYS_PROMPT_IN_FIRST_USER_MESSAGE=False,
    DEFAULT_STOP_SEQUENCES=["<|endsystem|>", "<|enduser|>", "<|endassistant|>"]
)

Contributing

We welcome contributions to the llama-cpp-agent framework! If you'd like to contribute, please follow these guidelines:

  1. Fork the repository and create your branch from master.
  2. Ensure your code follows the project's coding style and conventions.
  3. Write clear, concise commit messages and pull request descriptions.
  4. Test your changes thoroughly before submitting a pull request.
  5. Open a pull request to the master branch.

If you encounter any issues or have suggestions for improvements, please open an issue on the GitHub repository.

License

The llama-cpp-agent framework is released under the MIT License.

FAQ

Q: How do I install the optional dependencies for RAG?
A: To use the RAGColbertReranker class and the RAG example, you need to install the optional rag dependencies (ragatouille). You can do this by running pip install llama-cpp-agent[rag].

Q: Can I contribute to the llama-cpp-agent project?
A: Absolutely! We welcome contributions from the community. Please refer to the Contributing section for guidelines on how to contribute.

Q: Is llama-cpp-agent compatible with the latest version of llama-cpp-python?
A: Yes, llama-cpp-agent is designed to work with the latest version of llama-cpp-python. However, if you encounter any compatibility issues, please open an issue on the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llama_cpp_agent-0.2.0.tar.gz (64.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llama_cpp_agent-0.2.0-py3-none-any.whl (77.2 kB view details)

Uploaded Python 3

File details

Details for the file llama_cpp_agent-0.2.0.tar.gz.

File metadata

  • Download URL: llama_cpp_agent-0.2.0.tar.gz
  • Upload date:
  • Size: 64.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for llama_cpp_agent-0.2.0.tar.gz
Algorithm Hash digest
SHA256 82a9cbe90bce51361f63627295665036fa5ab289f8ae7b001bd2e9567def9c93
MD5 76d94583561df7472fd43e948537bb7d
BLAKE2b-256 b6142679f3c2b1f1699545063a231324266d8e059c9cc8ef6fd050ce8e8db05e

See more details on using hashes here.

File details

Details for the file llama_cpp_agent-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_cpp_agent-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1901b9c5751c04abca3d37089474aa7b828f701d00ed0d7c8687724cf545f04e
MD5 1a4f809fdc05154d6807d236191d4850
BLAKE2b-256 1777f61a40ac3deaaec7c20081e62ac50c4725d08ea02747ffbf3fc26c64005c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page