Skip to main content

A Python package for creating AI Assistants and AI Agents with Vectara

Project description

vectara-agentic

License Maintained Twitter Discord

Overview

vectara-agentic is a Python library for developing powerful AI assistants using Vectara and Agentic-RAG. It leverages the LlamaIndex Agent framework, customized for use with Vectara.

Key Features

  • Supports ReAct and OpenAIAgent agent types.
  • Includes pre-built tools for various domains (e.g., finance, legal).
  • Enables easy creation of custom AI assistants and agents.

Important Links

Prerequisites

Installation

pip install vectara-agentic

Quick Start

  1. Create a Vectara RAG tool
import os
from vectara_agentic import VectaraToolFactory

vec_factory = VectaraToolFactory(
    vectara_api_key=os.environ['VECTARA_API_KEY'],
    vectara_customer_id=os.environ['VECTARA_CUSTOMER_ID'],
    vectara_corpus_id=os.environ['VECTARA_CORPUS_ID']
)

class QueryFinancialReportsArgs(BaseModel):
        query: str = Field(..., description="The user query.")
        year: int = Field(..., description=f"The year. An integer between {min(years)} and {max(years)}.")
        ticker: str = Field(..., description=f"The company ticker. Must be a valid ticket symbol from the list {tickers.keys()}.")

query_financial_reports = vec_factory.create_rag_tool(
    tool_name="query_financial_reports",
    tool_description="Query financial reports for a company and year",
    tool_args_schema=QueryFinancialReportsArgs,
)
  1. Create other tools (optional)

In addition to RAG tools, you can generate a lot of other types of tools the agent can use. These could be mathematical tools, tools that call other APIs to get more information, or any other type of tool.

See Tools for more information.

  1. Create your agent
agent = Agent(
    tools = [query_financial_reports],
    topic = topic_of_expertise,
    custom_instructions = financial_bot_instructions,
)
  • tools is the list of tools you want to provide to the agent. In this example it's just a single tool.
  • topic is a string that defines the expertise you want the agent to specialize in.
  • custom_instructions is an optional string that defines special instructions to the agent.

For example, for a financial agent we might use:

topic_of_expertise = "10-K financial reports",

financial_bot_instructions = """
    - You are a helpful financial assistant in conversation with a user. Use your financial expertise when crafting a query to the tool, to ensure you get the most accurate information.
    - You can answer questions, provide insights, or summarize any information from financial reports.
    - A user may refer to a company's ticker instead of its full name - consider those the same when a user is asking about a company.
    - When calculating a financial metric, make sure you have all the information from tools to complete the calculation.
    - In many cases you may need to query tools on each sub-metric separately before computing the final metric.
    - When using a tool to obtain financial data, consider the fact that information for a certain year may be reported in the the following year's report.
    - Report financial data in a consistent manner. For example if you report revenue in thousands, always report revenue in thousands.
    """

Configuration

Configure vectara-agentic using environment variables:

  • VECTARA_AGENTIC_AGENT_TYPE: valid values are REACT, LLMCOMPILER or OPENAI (default: OPENAI)
  • VECTARA_AGENTIC_MAIN_LLM_PROVIDER: valid values are OPENAI, ANTHROPIC, TOGETHER, GROQ, COHERE or FIREWORKS (default: OPENAI)
  • VECTARA_AGENTIC_MAIN_MODEL_NAME: agent model name (default depends on provider)
  • VECTARA_AGENTIC_TOOL_LLM_PROVIDER: tool LLM provider (default: OPENAI)
  • VECTARA_AGENTIC_TOOL_MODEL_NAME: tool model name (default depends on provider)

Agent Tools

vectara-agentic provides a few tools out of the box:

  1. Standard tools:
  • summarize_text: a tool to summarize a long text into a shorter summary (uses LLM)
  • rephrase_text: a tool to rephrase a given text, given a set of rephrase instructions (uses LLM)
  1. Legal tools: a set of tools for the legal vertical, such as:
  • summarize_legal_text: summarize legal text with a certain point of view
  • critique_as_judge: critique a legal text as a judge, providing their perspective
  1. Financial tools: based on tools from Yahoo Finance:
  • tools to understand the financials of a public company like: balance_sheet, income_statement, cash_flow
  • stock_news: provides news about a company
  • stock_analyst_recommendations: provides stock analyst recommendations for a company.
  1. database_tools: providing a few tools to inspect and query a database
  • list_tables: list all tables in the database
  • describe_tables: describe the schema of tables in the database
  • load_data: returns data based on a SQL query

More tools coming soon.

You can create your own tool directly from a Python function using the create_tool() method of the ToolsFactor class:

def mult_func(x, y):
    return x*y

mult_tool = ToolsFactory().create_tool(mult_func)

Agent Diagnostics

The Agent class defines a few helpful methods to help you understand the internals of your application.

  • The report() method prints out the agent object’s type, the tools, and the LLMs used for the main agent and tool calling.
  • The token_counts() method tells you how many tokens you have used in the current session for both the main agent and tool calling LLMs. This can be helpful if you want to track spend by token.

Observability

vectara-agentic supports observability via the existing integration of LlamaIndex and Arize Phoenix. To enable tracing of your vectara-agentic assistant, follow these steps (adapted from here):

  1. Go to https://llamatrace.com/login an create an account, then create an API key and put it in the PHOENIX_API_KEY variable
  2. os["VECTARA_AGENTIC_OBSERVER_TYPE"] = "ARIZE_PHOENIX": to enable Arize Phoenix observability
  3. os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"api_key={PHOENIX_API_KEY}"

Now when you run your agent, all metrics are sent to LlamaTrace and recorded. You can view them at https://llamatrace.com. If you do not include the OTEL_EXPORTER_OTLP_HEADERS a local instance of Arize Phoenix will be setup instead and you can view it at http://localhost:6006

About Custom Instructions

The custom instructions you provide to the agent guide its behavior. Here are some guidelines when creating your instructions:

  • Write precise and clear instructions, without overcomplicating.
  • Consider edge cases and unusual or atypical scenarios.
  • Be cautious to not over-specify behavior based on your primary use-case, as it may limit the agent's ability to behave properly in others.

Examples

Check out our example AI assistants:

Contributing

We welcome contributions! Please see our contributing guide for more information.

License

This project is licensed under the Apache 2.0 License. See the LICENSE file for details.

Contact

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

vectara_agentic-0.1.9.tar.gz (26.9 kB view details)

Uploaded Source

Built Distribution

vectara_agentic-0.1.9-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file vectara_agentic-0.1.9.tar.gz.

File metadata

  • Download URL: vectara_agentic-0.1.9.tar.gz
  • Upload date:
  • Size: 26.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for vectara_agentic-0.1.9.tar.gz
Algorithm Hash digest
SHA256 0ebbb24ec5ee0669228d9b5e871dab010b58cbce1265a3ed88485e65c160ae0f
MD5 8002e76a1f4d8d12badfe9a811b01377
BLAKE2b-256 f97f109e077372df4e099217395d78d063ea0fc951e0b1c38b6e73a11ce516f3

See more details on using hashes here.

File details

Details for the file vectara_agentic-0.1.9-py3-none-any.whl.

File metadata

File hashes

Hashes for vectara_agentic-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 c38ffc520c2aa8c768710e044cd451a9e0faf050790521b42b82493a2f0c03d3
MD5 dbd6d4bbf242669607f8cc44d1cbd47c
BLAKE2b-256 8bb5d10035f8611d26d3f9e3773f456c6f4af2414000a16885d63665a36c633b

See more details on using hashes here.

Supported by

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