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-2.0.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-2.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: trelix_langchain-2.0.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-2.0.0.tar.gz
Algorithm Hash digest
SHA256 bc11c8cfe862b91812af97cb867ba055390822f9ddb573059821ca3d2374ed39
MD5 6e38545d595b6118596d4308e08a7cdf
BLAKE2b-256 8abda51808656618ee630b4b8140ef37de749b8791991e1500092326cd4f371e

See more details on using hashes here.

Provenance

The following attestation bundles were made for trelix_langchain-2.0.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-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for trelix_langchain-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 beed29c71c8d0bf4217956aae594b64b769d892d62592cbd95852dec367d0b85
MD5 4268cda0f8c2469acd6dbfdba98e4040
BLAKE2b-256 876657ce2a8457abeb74ab3ff563315680ab06689250cc720cc3ab1fafd3af7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for trelix_langchain-2.0.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