Work with providers
Use providers when you need to generate text or citation-backed responses from an LLM, and you want to swap between Claude and Gemini without changing your application code.
Prerequisites
- An API key for the provider you want to use (Anthropic for Claude, Google for Gemini)
- The matching optional SDK installed:
- Claude:
pip install attune-rag[claude] - Gemini:
pip install attune-rag[gemini]
- Claude:
Steps
Check which providers are available
-
Import
list_availablefromproviders:from providers import list_available print(list_available())list_available()returns the names of providers whose SDKs are currently importable. If a provider's SDK is not installed, it does not appear in the list.
Get a provider instance
-
Call
get_provider(name, **kwargs)with the provider name and any constructor arguments:from providers import get_provider provider = get_provider("claude", api_key="sk-...")Pass
api_keyto authenticate. If you omit it, the provider reads the key from its default environment variable.get_providerraisesValueErrorif the name is not recognized, with a message listing the known providers.
Generate text
-
Call
generateon the provider instance:response = await provider.generate( prompt="Summarize the history of functional programming.", model=None, # uses the provider's default model max_tokens=2048, ) print(response)Pass
cached_prefixif you want to reuse a long shared context across multiple calls.
Generate a response with citations
-
Build a list of
CitationDocumentobjects, one per source document:from providers.base import CitationDocument documents = [ CitationDocument(title="SICP", text="...chapter text..."), CitationDocument(title="TAPL", text="...chapter text..."), ] -
Call
generate_with_citationson a provider that supports it (for example,ClaudeProvider):result = await provider.generate_with_citations( documents=documents, query="What is a lambda calculus?", system="You are a computer science tutor.", max_tokens=2048, )The method returns a
CitedResponsewith two fields:text— the generated answerclaim_citations— a tuple ofClaimCitationobjects linking claims in the answer to source documents
Verify the task worked
list_available()returns a non-empty list that includes your target provider name.get_provider(name)returns an object without raisingValueError.generate(...)returns a non-empty string.- For citation calls,
result.textis non-empty andresult.claim_citationscontains at least one entry when the answer draws on the supplied documents.