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

Uploaded Python 3

File details

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

File metadata

  • Download URL: trelix_langchain-1.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-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4bb61e23980f3a2e694d273d310388c2c4a0821fe448b9ec0a9cd13cdfd85165
MD5 5a6e89c06daee9518c47d01b460cf37d
BLAKE2b-256 9f570b2d4296d25f5d966cdcdd559ac12decc782bf58bf9e638d863985e9f510

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for trelix_langchain-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e140101ea95bc2a037c0eb81936675de9dd1d229fa86405df68c499ab34fc07
MD5 4ae48355f302ae8e430f57e865bec85d
BLAKE2b-256 68e0c6703ba9c2d0dade63806faa2d455b802b8cdd81b2ea16285b73503d6931

See more details on using hashes here.

Provenance

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