Skip to main content

llama-index packs for chain-of-abstraction

Project description

Chain-of-Abstraction Agent Pack

pip install llama-index-packs-agents-coa

The chain-of-abstraction (CoA) LlamaPack implements a generalized version of the strategy described in the origin CoA paper.

By prompting the LLM to write function calls in a chain-of-thought format, we can execute both simple and complex combinations of function calls needed to execute a task.

The LLM is prompted to write a response containing function calls, for example, a CoA plan might look like:

After buying the apples, Sally has [FUNC add(3, 2) = y1] apples.
Then, the wizard casts a spell to multiply the number of apples by 3,
resulting in [FUNC multiply(y1, 3) = y2] apples.

From there, the function calls can be parsed into a dependency graph, and executed.

Then, the values in the CoA are replaced with their actual results.

As an extension to the original paper, we also run the LLM a final time, to rewrite the response in a more readable and user-friendly way.

NOTE: In the original paper, the authors fine-tuned an LLM specifically for this, and also for specific functions and datasets. As such, only capabale LLMs (OpenAI, Anthropic, etc.) will be (hopefully) reliable for this without finetuning.

Code Usage

pip install llama-index-packs-agents-coa

First, setup some tools (could be function tools, query engines, etc.)

from llama_index.core.tools import QueryEngineTool, FunctionTool


def add(a: int, b: int) -> int:
    """Add two numbers together."""
    return a + b


query_engine = index.as_query_engine(...)

function_tool = FunctionTool.from_defaults(fn=add)
query_tool = QueryEngineTool.from_defaults(
    query_engine=query_engine, name="...", description="..."
)

Next, create the pack with the tools, and run it!

from llama_index.packs.agents_coa import CoAAgentPack
from llama_index.llms.openai import OpenAI

pack = CoAAgentPack(
    tools=[function_tool, query_tool], llm=OpenAI(model="gpt-4")
)

print(pack.run("What is 1245 + 4321?"))

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_index_packs_agents_coa-0.3.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

llama_index_packs_agents_coa-0.3.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_packs_agents_coa-0.3.1.tar.gz.

File metadata

File hashes

Hashes for llama_index_packs_agents_coa-0.3.1.tar.gz
Algorithm Hash digest
SHA256 e2e4e967ec90542456733550d591a1781292282836b4d86242f644f7e62a381b
MD5 a05d8de93b6562f6830c5660b068d692
BLAKE2b-256 f70ff626487bbf7eb2069075b9d08151397b01c886e0f559c56a01251aa0cb65

See more details on using hashes here.

File details

Details for the file llama_index_packs_agents_coa-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_packs_agents_coa-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 03356010ca5d79d01866d902e48bdd87db7bd72e4e706d224ba67c9d3c0089d7
MD5 e38f812204cd81ee28182978b9e0c410
BLAKE2b-256 eb1c5061695694bd37c48e7ce9ac56cd4ada64ace7bcf03b46902e0968b9ba95

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