|
| 1 | +--- |
| 2 | +title: "Observe LangChain Application" |
| 3 | +--- |
| 4 | + |
| 5 | +This guide provides a detailed walkthrough of setting up and using Observe in your LangChain projects. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +### **1. Install FutureAGI** |
| 10 | +Install the FutureAGI package, which includes the Observe framework. |
| 11 | +```bash |
| 12 | +pip install futureagi |
| 13 | +``` |
| 14 | +This package provides the essential tools for tracing, monitoring, and evaluating LangChain applications. |
| 15 | + |
| 16 | +### 2. Configure Environment Variables |
| 17 | +Set up your environment with the necessary API keys for OpenAI and FutureAGI. These credentials are crucial for: |
| 18 | +- Accessing OpenAI's language models. |
| 19 | +- Authenticating with FutureAGI's observability services. |
| 20 | +- Securely transmitting telemetry data. |
| 21 | + |
| 22 | +```python |
| 23 | +import os |
| 24 | +os.environ["OPENAI_API_KEY"] = "your-openai-api-key" |
| 25 | +os.environ["FI_API_KEY"] = "your-futureagi-api-key" |
| 26 | +os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key" |
| 27 | +``` |
| 28 | + |
| 29 | +### 3. Initialize Trace Provider |
| 30 | +The trace provider is the core component that enables observability. |
| 31 | +- Creates a new project in the Observe platform |
| 32 | +- Establishes a connection for sending telemetry data |
| 33 | +- Sets up versioning for tracking changes over time |
| 34 | +- Initializes OpenTelemetry integration for distributed tracing |
| 35 | + |
| 36 | +```python |
| 37 | +from fi.integrations.otel import register |
| 38 | +from fi.integrations.otel.types import ProjectType |
| 39 | + |
| 40 | +trace_provider = register( |
| 41 | + project_type=ProjectType.OBSERVE, |
| 42 | + project_name="langchain_demo", |
| 43 | + project_version_name="v1" |
| 44 | +) |
| 45 | +``` |
| 46 | + |
| 47 | +### 4. Setup LangChain Instrumentation |
| 48 | +Configure automatic instrumentation for your LangChain components. The instrumentation: |
| 49 | +- Automatically captures all LangChain operations |
| 50 | +- Tracks prompt templates and their parameters |
| 51 | +- Monitors model calls and responses |
| 52 | +- Records timing information for each step |
| 53 | +- Enables detailed tracing of the chain execution |
| 54 | + |
| 55 | +```python |
| 56 | +from fi.integrations.otel import LangChainInstrumentor |
| 57 | +from langchain_core.prompts import ChatPromptTemplate |
| 58 | +from langchain_openai import ChatOpenAI |
| 59 | + |
| 60 | +LangChainInstrumentor().instrument(tracer_provider=trace_provider) |
| 61 | + |
| 62 | +prompt = ChatPromptTemplate.from_template("{x} {y} {z}?").partial(x="why is", z="blue") |
| 63 | +chain = prompt | ChatOpenAI(model_name="gpt-3.5-turbo") |
| 64 | +``` |
| 65 | + |
| 66 | +### 5. Execute Chain with Observability |
| 67 | +Run your chain with full observability enabled: |
| 68 | + |
| 69 | +```python |
| 70 | +def run_chain(): |
| 71 | + try: |
| 72 | + result = chain.invoke({"y": "sky"}) |
| 73 | + print(f"Response: {result}") |
| 74 | + except Exception as e: |
| 75 | + print(f"Error executing chain: {e}") |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + run_chain() |
| 79 | +``` |
| 80 | +During execution: |
| 81 | +- Each step of the chain is automatically traced |
| 82 | +- Prompts and completions are logged |
| 83 | +- Errors are captured and recorded |
| 84 | +- Performance metrics are collected |
| 85 | +- All data is sent to your Observe dashboard |
| 86 | + |
| 87 | +--- |
| 88 | +With the instrumentation setup from SDK, all telemetry data flows into Observe's UI, where you can interact with real-time dashboards, apply filters, and analyze performance. |
| 89 | + |
| 90 | + |
| 91 | +### 6. LLM Tracing |
| 92 | +LLM Tracing is pivotal for obtaining granular insights into your application's interactions with language models. After adding a project to the observability platform, users can access a detailed view of trace information, which includes: |
| 93 | + |
| 94 | +- **Node Type**: Specifies the type of operation (e.g., input handling, model inference). |
| 95 | +- **Trace ID**: A unique identifier that allows you to track individual interactions. |
| 96 | +- **Input/Output**: Displays the input provided to the model along with its output. |
| 97 | + |
| 98 | +You can also view the evaluation metrics for your traces here. You can setup the evals from the [Evals & Tasks](#8-evals-and-tasks) section. These metrics quantify response quality, allowing you to apply evaluations that compute key statistics, such as count, average, and standard deviation—and visualize insights through interactive graphs. |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +### 7. Sessions |
| 103 | +Sessions enable to group and manage related tracing activities, providing comprehensive insights into the interaction history between users and AI. This includes: |
| 104 | +- **Timeframe Filtering**: You can filter sessions by specific time intervals, making trend analysis straightforward. |
| 105 | +- **Detailed Interaction Histories**: Access a chronological log of interactions within a session, crucial for understanding conversation flows in applications like chatbots. |
| 106 | +- **Contextual Analysis**: View complete session data to analyze user behavior and model response patterns effectively. |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +### 8. Evals & Tasks |
| 111 | +This section automates model evaluation and monitoring, streamlining performance analysis by enabling you to define sampling rates, apply filters, set evaluation criteria, and manage tasks. By specifying the percentage of data to evaluate, you can optimize resource efficiency while ensuring meaningful insights. |
| 112 | + |
| 113 | +[Click here to learn about all the evals provided by FutureAGI.]( /future-agi/products/evaluation/eval-definition/overview) |
| 114 | + |
| 115 | +Filters allow you to narrow evaluations to specific data subsets based on custom criteria, providing a targeted approach to analysis. Evaluation criteria can be established to assess model performance comprehensively, incorporating key metrics that reflect accuracy, consistency, and relevance. |
| 116 | + |
| 117 | +--- |
| 118 | +With features like detailed LLM Tracing, session-centric data analysis, and automated evaluation tasks, Observe empowers teams to maintain high-quality, reliable AI systems. |
| 119 | + |
| 120 | +--- |
0 commit comments