Skip to main content

trelix-langchain

LangChain retriever for trelix — semantic code search using Tree-sitter AST parsing, hybrid BM25+vector search, and call-graph expansion.

Install

pip install trelix-langchain

For AWS Bedrock embeddings (Cohere or Titan):

pip install "trelix-langchain[bedrock]"

Basic Usage

from trelix_langchain import TrelixRetriever

# First index your repo (one-time)
# trelix index /path/to/repo

retriever = TrelixRetriever(repo_path="/path/to/repo", k=10)
docs = retriever.invoke("how does authentication work?")

for doc in docs:
    print(doc.metadata["source"], doc.metadata["score"])
    print(doc.page_content[:200])

Each returned Document carries rich metadata:

Metadata key Example value
source "src/auth/middleware.py"
symbol "auth.middleware.require_login"
language "python"
kind "function"
lines "42-78"
score 0.91
retrieval_source "hybrid"

LangChain RAG Chain (LCEL)

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from trelix_langchain import TrelixRetriever

retriever = TrelixRetriever(repo_path="/path/to/repo", k=8)

prompt = ChatPromptTemplate.from_template(
    "Answer the question using only the code context below.\n\n"
    "Context:\n{context}\n\n"
    "Question: {question}"
)

def format_docs(docs):
    return "\n\n".join(
        f"# {d.metadata['source']} ({d.metadata['symbol']})\n{d.page_content}"
        for d in docs
    )

chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | ChatOpenAI(model="gpt-4o")
    | StrOutputParser()
)

answer = chain.invoke("How does the authentication middleware work?")
print(answer)

RetrievalQA (classic interface)

from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
from trelix_langchain import TrelixRetriever

retriever = TrelixRetriever(repo_path="/path/to/repo", k=10)
llm = ChatOpenAI(model="gpt-4o")

qa = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    return_source_documents=True,
)

result = qa.invoke({"query": "Where is rate limiting applied?"})
print(result["result"])
for doc in result["source_documents"]:
    print(" -", doc.metadata["source"])

Configuration

Env var Default Description
TRELIX_EMBEDDER_PROVIDER local Embedding provider: local | local-code | openai | azure | voyage | bedrock-cohere | bedrock-titan
OPENAI_API_KEY Required for openai provider
AZURE_API_KEY Required for azure provider
AWS_ACCESS_KEY_ID Required for Bedrock providers
AWS_SECRET_ACCESS_KEY Required for Bedrock providers
AWS_DEFAULT_REGION us-east-1 AWS region for Bedrock

You can also set the provider directly on the retriever instance:

retriever = TrelixRetriever(repo_path="/path/to/repo", provider="openai", k=10)

Provider Switching (v0.7.0+)

# Use Bedrock Cohere embeddings (best retrieval quality, reuses AWS credentials)
TRELIX_EMBEDDER_PROVIDER=bedrock-cohere trelix index /path/to/repo

# Use Azure OpenAI embeddings
TRELIX_EMBEDDER_PROVIDER=azure trelix index /path/to/repo

# Use local sentence-transformers (no API key needed, works offline)
TRELIX_EMBEDDER_PROVIDER=local trelix index /path/to/repo

The index and the retriever must use the same provider — re-index whenever you switch.

Links

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trelix_langchain-1.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

trelix_langchain-1.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file trelix_langchain-1.1.0.tar.gz.

File metadata

  • Download URL: trelix_langchain-1.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for trelix_langchain-1.1.0.tar.gz
Algorithm Hash digest
SHA256 47af261b9a5bf554862a5a558336acf5a50bb3e2758fe6d67a02427ec8af628a
MD5 f7428001db44a2b4fcb45b19fe83cf9e
BLAKE2b-256 90f43c5300ba2c83613c95032abf9adb8d3bd65f7fd4d648380c735c1a03d0d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for trelix_langchain-1.1.0.tar.gz:

Publisher: release.yml on sairam0424/trelix

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trelix_langchain-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for trelix_langchain-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4151152c58f7b7f8fef72b8a14f19d74776343b7ed6addc9e5a2d7241261529
MD5 32ad5c3b00e421d5ff2bc5e505474176
BLAKE2b-256 33c576c00d028404ebb5df250167b25bf3c526b93627678a03114b0abee92289

See more details on using hashes here.

Provenance

The following attestation bundles were made for trelix_langchain-1.1.0-py3-none-any.whl:

Publisher: release.yml on sairam0424/trelix

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page