Skip to main content

llama-index selectors Not Diamond integration

Project description

LlamaIndex Selectors Integration: NotDiamond

Not Diamond offers an AI-powered model router that automatically determines which LLM is best suited to respond to any query, improving LLM output quality by combining multiple LLMs into a meta-model that learns when to call each LLM.

Not Diamond supports cost and latency tradeoffs, customized router training, and real-time router personalization. Learn more via the documentation.

Installation

pip install llama-index-selectors-notdiamond

Configuration

  • API keys: You need API keys from Not Diamond and any LLM you want to use.

Quick Start

import os
from typing import List

from llama_index.core import (
    SimpleDirectoryReader,
    VectorStoreIndex,
    SummaryIndex,
    Settings,
)
from llama_index.core.query_engine import RouterQueryEngine
from llama_index.core.tools import QueryEngineTool
from llama_index.selectors.notdiamond.base import NotDiamondSelector

from notdiamond import NotDiamond


# Set up your API keys
os.environ["OPENAI_API_KEY"] = "sk-..."
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."
os.environ["NOTDIAMOND_API_KEY"] = "sk-..."


# Create indexes
documents = SimpleDirectoryReader("data/paul_graham").load_data()
nodes = Settings.node_parser.get_nodes_from_documents(documents)

vector_index = VectorStoreIndex.from_documents(documents)
summary_index = SummaryIndex.from_documents(documents)
query_text = "What was Paul Graham's role at Yahoo?"


# Set up Tools for the QueryEngine
list_query_engine = summary_index.as_query_engine(
    response_mode="tree_summarize",
    use_async=True,
)
vector_query_engine = vector_index.as_query_engine()

list_tool = QueryEngineTool.from_defaults(
    query_engine=list_query_engine,
    description=(
        "Useful for summarization questions related to Paul Graham eassy on"
        " What I Worked On."
    ),
)

vector_tool = QueryEngineTool.from_defaults(
    query_engine=vector_query_engine,
    description=(
        "Useful for retrieving specific context from Paul Graham essay on What"
        " I Worked On."
    ),
)


# Create a NotDiamondSelector and RouterQueryEngine
client = NotDiamond(
    api_key=os.environ["NOTDIAMOND_API_KEY"],
    llm_configs=["openai/gpt-4o", "anthropic/claude-3-5-sonnet-20240620"],
)
preference_id = client.create_preference_id()
client.preference_id = preference_id

nd_selector = NotDiamondSelector(client=client)

query_engine = RouterQueryEngine(
    selector=nd_selector,
    query_engine_tools=[
        list_tool,
        vector_tool,
    ],
)


# Use Not Diamond to Query Indexes
response = query_engine.query(
    "Please summarize Paul Graham's working experience."
)
print(str(response))

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_selectors_notdiamond-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file llama_index_selectors_notdiamond-0.1.0.tar.gz.

File metadata

File hashes

Hashes for llama_index_selectors_notdiamond-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f7eef1c74f3cfd5a936654e082b5c8d0dc0660170f6532dcbd9f2c559cd679c4
MD5 b72fd7442a8052cfda7a7cde701db1f1
BLAKE2b-256 6ed33c0df19757794e2ee3760fb46512e008eac10a620d6a33a97170a4427604

See more details on using hashes here.

File details

Details for the file llama_index_selectors_notdiamond-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_selectors_notdiamond-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90a486ac96a5bc8673170ce86b11fadd025b9554131e721ccf6b611daab93321
MD5 18cd84b6e402ee9694ba6e985e2a6cd1
BLAKE2b-256 689f08871812a17f49a3eea7caad5c40a23c2f7d610905a92eacaf32fef8d7b8

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