You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ReadMe.md
+114-3
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# llama-cpp-agent Framework
2
2
3
3
## Introduction
4
-
The llama-cpp-agent framework is a tool designed for easy interaction with Large Language Models (LLMs). Allowing users to chat with LLM models, execute structured function calls and get structured output (objects).
4
+
The llama-cpp-agent framework is a tool designed for easy interaction with Large Language Models (LLMs). Allowing users to chat with LLM models, execute structured function calls, get structured output (objects) and do retrieval augmented generation.
5
5
6
6
It provides a simple yet robust interface and supports llama-cpp-python and OpenAI endpoints with GBNF grammar support (like the llama-cpp-python server) and the llama.cpp backend server.
7
7
It works by generating a formal GGML-BNF grammar of the user defined structures and functions, which is then used by llama.cpp to generate text valid to that grammar. In contrast to most GBNF grammar generators it also supports nested objects, dictionaries, enums and lists of them.
@@ -10,6 +10,7 @@ It works by generating a formal GGML-BNF grammar of the user defined structures
10
10
-**Simple Chat Interface**: Engage in seamless conversations with LLMs.
11
11
-**Structured Output**: Get structured output (objects) from LLMs.
12
12
-**Single and Parallel Function Calling**: Let the LLM execute functions.
-**Flexibility**: Suited for various applications from casual chatting to specific function executions.
14
15
15
16
## Installation
@@ -75,7 +76,6 @@ while True:
75
76
76
77
```python
77
78
# Example that uses the FunctionCallingAgent class to create a function calling agent.
78
-
# Example that uses the FunctionCallingAgent class to create a function calling agent.
79
79
import datetime
80
80
from enum import Enum
81
81
from typing import Union, Optional
@@ -111,7 +111,7 @@ class MathOperation(Enum):
111
111
112
112
113
113
# llama-cpp-agent also supports "Instructor" library like function definitions as Pydantic models for function calling.
114
-
# Simple pydantic calculator tool for the agent that can add, subtract, multiply, and divide. Docstring and description of fields will be used in system prompt.
114
+
# Simple pydantic calculator tool for the agent that can add, subtract, multiply, and divide. Docstring and description of fields will be used in the system prompt.
115
115
classcalculator(BaseModel):
116
116
"""
117
117
Perform a math operation on two numbers.
@@ -299,6 +299,117 @@ title='The Feynman Lectures on Physics' author='Richard Feynman, Robert B. Leigh
299
299
300
300
```
301
301
302
+
### RAG - Retrieval Augmented Generation
303
+
This example shows how to do RAG with colbert reranking.
304
+
```python
305
+
import json
306
+
307
+
from ragatouille.utils import get_wikipedia_page
308
+
309
+
from llama_cpp_agent.messages_formatter import MessagesFormatterType
310
+
311
+
from typing import List
312
+
313
+
from pydantic import BaseModel, Field
314
+
315
+
from llama_cpp_agent.llm_agent import LlamaCppAgent
316
+
from llama_cpp_agent.gbnf_grammar_generator.gbnf_grammar_from_pydantic_models import (
317
+
generate_gbnf_grammar_and_documentation,
318
+
)
319
+
from llama_cpp_agent.providers.llama_cpp_endpoint_provider import (
320
+
LlamaCppEndpointSettings,
321
+
)
322
+
from llama_cpp_agent.rag.rag_colbert_reranker import RAGColbertReranker
323
+
from llama_cpp_agent.rag.text_utils import RecursiveCharacterTextSplitter
324
+
325
+
326
+
# Initialize the chromadb vector database with a colbert reranker.
327
+
rag = RAGColbertReranker(persistent=False)
328
+
329
+
# Initialize a recursive character text splitter with the correct chunk size of the embedding model.
330
+
length_function =len
331
+
splitter = RecursiveCharacterTextSplitter(
332
+
separators=["\n\n", "\n", "", ""],
333
+
chunk_size=512,
334
+
chunk_overlap=0,
335
+
length_function=length_function,
336
+
keep_separator=True
337
+
)
338
+
339
+
# Use the ragatouille helper function to get the content of a wikipedia page.
340
+
page = get_wikipedia_page("Synthetic_diamond")
341
+
342
+
# Split the text of the wikipedia page into chunks for the vector database.
343
+
splits = splitter.split_text(page)
344
+
345
+
# Add the splits into the vector database
346
+
for split in splits:
347
+
rag.add_document(split)
348
+
349
+
# Define the query we want to ask based on the retrieved information
350
+
query ="What is a BARS apparatus?"
351
+
352
+
# Define a pydantic class to represent a query extension as additional queries to the original query.
353
+
classQueryExtension(BaseModel):
354
+
"""
355
+
Represents an extension of a query as additional queries.
356
+
"""
357
+
queries: List[str] = Field(default_factory=list, description="List of queries.")
358
+
359
+
360
+
# Generate a grammar and documentation of the query extension model.
# Define a query extension agent which will extend the query with additional queries.
367
+
query_extension_agent = LlamaCppAgent(
368
+
main_model,
369
+
debug_output=True,
370
+
system_prompt="You are a world class query extension algorithm capable of extending queries by writing new queries. Do not answer the queries, simply provide a list of additional queries in JSON format. Structure your output according to the following model:\n\n"+ docs.strip(),
BARS (Bridgman-Anvil High Pressure Reactor System) apparatus is a type of diamond-producing press used in the HPHT (High Pressure High Temperature) method for synthetic diamond growth. It consists of a ceramic cylindrical "synthesis capsule" placed in a cube of pressure-transmitting material, which is pressed by inner anvils and outer anvils. The whole assembly is locked in a disc-type barrel filled with oil, which pressurizes upon heating, and the oil pressure is transferred to the central cell. The BARS apparatus is claimed to be the most compact, efficient, and economical press design for diamond synthesis.
412
+
```
302
413
303
414
### Knowledge Graph Creation Example
304
415
This example, based on an example of the Instructor library for OpenAI,
Copy file name to clipboardExpand all lines: examples/06_Rag/example_synthetic_diamonds_bars.py
+4-4
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,7 @@ class QueryExtension(BaseModel):
75
75
query_extension_agent=LlamaCppAgent(
76
76
main_model,
77
77
debug_output=True,
78
-
system_prompt="You are a world class query extension algorithm capable of extending queries by writing new queries. Do not answer the queries, simply provide a list of additional queries in JSON format. Structure your output according to the following model:\n\n"+docs,
78
+
system_prompt="You are a world class query extension algorithm capable of extending queries by writing new queries. Do not answer the queries, simply provide a list of additional queries in JSON format. Structure your output according to the following model:\n\n"+docs.strip(),
0 commit comments