Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Truera Trulens TruBot App

From Leeroopedia
Knowledge Sources
Domains LLM Applications
Last Updated 2026-02-14 08:00 GMT

Overview

A LangChain-based conversational chatbot (TruBot) that uses Pinecone for vector retrieval and OpenAI for question answering, integrated with Slack and instrumented with TruLens feedback functions.

Description

The TruBot application, defined in examples/experimental/end2end_apps/trubot/trubot.py, is a complete end-to-end conversational bot that combines several technologies:

  • Pinecone -- Used as a vector database for document retrieval. The bot connects to an existing Pinecone index named "llmdemo" using OpenAI embeddings (text-embedding-ada-002).
  • LangChain -- Provides the ConversationalRetrievalChain framework that ties together the retriever, LLM, and conversation memory into a unified chain.
  • OpenAI -- Powers the language model (OpenAI with temperature 0 and 128 max tokens) for generating answers.
  • Slack -- The bot operates as a Slack application using slack_bolt, handling direct messages and mentions.
  • TruLens -- Instruments the chain via TruChain and evaluates quality using three feedback functions.

The bot supports five app variants (selectable per conversation) that experiment with different prompt strategies:

  • 0/default -- The standard LangChain prompt.
  • 1/lang_prompt -- Modified prompt that instructs the LLM to respond in the same language as the question.
  • 2/relevance_prompt -- Modified prompt that instructs the LLM to use only relevant contexts.
  • 3/filtered_context -- Uses TruLens WithFeedbackFilterDocuments to filter retrieved documents below a relevance threshold of 0.5.
  • 4/filtered_context_and_lang_prompt -- Combines filtered context with the language-matching prompt.

Three TruLens feedback functions are defined:

  • f_lang_match -- Evaluates language match between question and answer using HuggingFace.
  • f_qa_relevance -- Evaluates question/answer relevance using OpenAI.
  • f_context_relevance -- Evaluates question/context chunk relevance using OpenAI, aggregated with np.min.

Usage

This application is used as an experimental end-to-end demo of TruLens instrumentation on a production-like chatbot. It demonstrates how to set up feedback functions, instrument LangChain chains with TruChain, and compare multiple app variants for conversational quality evaluation. It is also a practical example of integrating TruLens with Slack bots.

Code Reference

Source Location

Signature

def get_or_make_app(
    cid: str,
    selector: int = 0,
    feedback_mode=feedback_schema.FeedbackMode.DEFERRED,
) -> mod_tru_chain.TruChain: ...

def get_answer(
    app: mod_tru_chain.TruChain,
    question: str,
) -> Tuple[str, str]: ...

def answer_message(client, body: dict, logger): ...

def start_bot(): ...

Import

from examples.experimental.end2end_apps.trubot.trubot import get_or_make_app, get_answer

I/O Contract

Function: get_or_make_app

Direction Name Type Description
Input cid str Slack conversation ID (channel ID or thread timestamp).
Input selector int App variant selector (0-4); determines prompt strategy and retrieval filtering.
Input feedback_mode FeedbackMode Feedback evaluation mode; defaults to DEFERRED.
Output return TruChain A TruLens-instrumented LangChain conversational retrieval chain.

Function: get_answer

Direction Name Type Description
Input app TruChain The TruLens-instrumented chain to use for answering.
Input question str The user question to answer.
Output return Tuple[str, str] A tuple of (answer text, sources elaboration text).

Function: answer_message

Direction Name Type Description
Input client WebClient Slack API WebClient instance for posting messages.
Input body dict Slack event body containing message details (text, channel, user, timestamp).
Input logger Logger Logger instance for recording event activity.
Output (side effect) -- Posts answer and sources back to the Slack channel/thread.

Required Environment Variables

Variable Description
OPENAI_API_KEY API key for OpenAI services.
HUGGINGFACE_API_KEY API key for HuggingFace inference.
PINECONE_API_KEY API key for Pinecone vector database.
PINECONE_ENV Pinecone environment identifier.
SLACK_TOKEN Slack bot OAuth token.
SLACK_SIGNING_SECRET Slack app signing secret for request verification.

App Variants

Selector Version Description
0 0/default Standard LangChain default prompt.
1 1/lang_prompt Prompt instructs LLM to respond in same language as the question.
2 2/relevance_prompt Prompt instructs LLM to use only relevant contexts.
3 3/filtered_context Retriever filters documents below relevance threshold 0.5.
4 4/filtered_context_and_lang_prompt Combines filtered context with language-matching prompt.

Usage Examples

from examples.experimental.end2end_apps.trubot.trubot import get_or_make_app, get_answer

# Create or retrieve a TruChain app for a given conversation
tc = get_or_make_app(cid="C0123456789", selector=1)

# Get an answer from the instrumented chain
answer_text, sources_text = get_answer(tc, "What is TruLens?")
print(answer_text)
print(sources_text)

To run the full Slack bot:

# Set required environment variables, then:
python examples/experimental/end2end_apps/trubot/trubot.py
# Bot starts on port 3000 and listens for Slack events

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment