### Getting Started with Langtrace Cloud Source: https://github.com/Scale3-Labs/langtrace Information on how to get started with Langtrace Cloud, a platform for managing and analyzing traces. ```markdown ## 🚀 Getting Started ### Langtrace Cloud ☁️ (Further details on getting started with Langtrace Cloud would typically follow here, but are not present in the provided text.) ``` -------------------------------- ### Install Langtrace SDK Source: https://docs.langtrace.ai/quickstart Installs the Langtrace SDK for TypeScript and Python applications using npm and pip respectively. ```typescript // Install the SDK npm i @langtrase/typescript-sdk ``` ```python # Install the SDK pip install langtrace-python-sdk ``` -------------------------------- ### Setup and Installation Source: https://docs.langtrace.ai/supported-integrations/vector-stores/milvus Commands to set up a Python virtual environment, install the Langtrace SDK and Milvus Python SDK, and load environment variables. ```bash python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate` ``` ```bash pip install langtrace-python-sdk pymilvus python-dotenv ``` -------------------------------- ### Langtrace SDK Integration Source: https://docs.langtrace.ai/introduction Instructions on how to get started with Langtrace by signing up, generating an API key, and installing the SDK into your project to begin shipping traces. ```APIDOC Langtrace SDK Integration: 1. Signup and generate an API key from the Langtrace platform. 2. Install the Langtrace SDK into your project: - For Python: `pip install langtrace-sdk` - For Typescript: `npm install langtrace-sdk` or `yarn add langtrace-sdk` 3. Import and initialize the SDK in your application code: - Python Example: ```python from langtrace_sdk import init_tracing init_tracing(api_key='YOUR_API_KEY') ``` - Typescript Example: ```typescript import { initTracing } from 'langtrace-sdk'; initTracing({ apiKey: 'YOUR_API_KEY' }); ``` This process enables Langtrace to collect and analyze traces from your LLM applications. ``` -------------------------------- ### Setup Virtual Environment and Install SDKs Source: https://docs.langtrace.ai/supported-integrations/otel-support/python-http-json Creates a project directory, sets up a Python virtual environment, and installs the OpenAI and Langtrace Python SDKs. ```bash mkdir langtrace-otel cd langtrace-otel touch langtrace_otel.py python3 -m venv venv source venv/bin/activate pip install openai langtrace-python-sdk ``` -------------------------------- ### Install OpenTelemetry and Bootstrap Source: https://docs.langtrace.ai/supported-integrations/observability-tools/grafana Installs the OpenTelemetry Python package with OTLP support and bootstraps the environment for instrumentation. ```bash pip install opentelemetry-distro[otlp] opentelemetry-bootstrap -a install ``` -------------------------------- ### Install Langtrace with Helm Source: https://docs.langtrace.ai/hosting/hosting_options/kubernetes Installs the Langtrace application on Kubernetes using the official Helm chart. This command adds the Langtrace Helm repository and then performs the installation. ```bash helm repo add langtrace https://Scale3-Labs.github.io/langtrace-helm-chart helm install langtrace langtrace/langtrace ``` -------------------------------- ### Project Setup and Dependencies Source: https://docs.langtrace.ai/supported-integrations/otel-support/ts-http-json Provides steps to set up a new TypeScript project, configure it for ES modules, install dependencies including Langtrace SDK, OpenAI, and the OTLP HTTP exporter, and set the OpenAI API key. ```bash mkdir langtrace-otel cd langtrace-otel touch langtrace_otel.js npm init -y ``` ```json { ... "type": "module", ... } ``` ```bash npm i openai @langtrase/typescript-sdk @opentelemetry/exporter-trace-otlp-http ``` ```bash export OPENAI_API_KEY="your-openai-api-key" ``` -------------------------------- ### Run LiteLLM Proxy with Configuration Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/litellm Command to start the LiteLLM Proxy server using a specified configuration file and enabling detailed debugging output. ```bash litellm --config config.yaml --detailed_debug ``` -------------------------------- ### Install Langtrace SDK and Langchain Dependencies Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/langchain Installs the necessary Python SDK for Langtrace and related Langchain libraries. Also includes installation for Pinecone client in TypeScript. ```bash pip install -U langtrace-python-sdk langchain langchain-chroma langchainhub ``` ```typescript npm install @pinecone-database/pinecone-client ``` -------------------------------- ### Setup Python Environment and Install Dependencies Source: https://docs.langtrace.ai/supported-integrations/llm-tools/kubeai Creates a Python virtual environment and installs the Langtrace Python SDK and OpenAI library. ```bash python3 -m venv .venv source .venv/bin/activate pip install langtrace-python-sdk openai ``` -------------------------------- ### Langtrace Self-Hosting Setup Source: https://github.com/Scale3-Labs/langtrace Instructions for self-hosting Langtrace. This involves setting up the environment, configuring the .env file, starting the servers, and taking down the setup. ```bash # Example for setting up Langtrace self-hosted # Refer to the documentation for detailed steps. # 1. Clone the repository # git clone https://github.com/Scale3-Labs/langtrace.git # cd langtrace # 2. Set up the .env file (example structure) # cp .env.example .env # Update .env with your specific configurations (database URLs, etc.) # 3. Start the servers (example commands) # docker compose up -d # 4. Take down the setup # docker compose down ``` -------------------------------- ### Install SwarmZero SDK Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/swarmzero Installs the SwarmZero Python SDK using pip. This is a prerequisite for integrating SwarmZero with Langtrace. ```bash pip install swarmzero ``` -------------------------------- ### Langtrace Usage Example Source: https://docs.langtrace.ai/quickstart Demonstrates a basic usage of Langtrace with the OpenAI client in Python and TypeScript. ```python from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"} ] ) print(response.choices[0].message) ``` ```typescript import OpenAI from 'openai'; const openai = new OpenAI(); async function main() { const completion = await openai.chat.completions.create({ messages: [ { role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: 'Hello!' } ], model: 'gpt-3.5-turbo', }); console.log(completion.choices[0].message.content); } main(); ``` -------------------------------- ### Start Langtrace Services Source: https://docs.langtrace.ai/hosting/hosting_options/docker-compose Starts all services defined in the docker-compose.yml file in detached mode. The application will be accessible at http://localhost:3000. ```bash docker compose up -d ``` -------------------------------- ### Install and Initialize Langtrace SDK Source: https://docs.langtrace.ai/supported-integrations/observability-tools/grafana Installs the Langtrace Python SDK and initializes it for use. It also notes that a Langtrace API key is not required for sending traces to Grafana. ```python # Install the SDK pip install langtrace-python-sdk # Import it into your project from langtrace_python_sdk import langtrace # Must precede any llm module imports langtrace.init(api_key = 'LANGTRACE_API_KEY') ``` -------------------------------- ### Install Dependencies Source: https://docs.langtrace.ai/hosting/using_local_setup Installs the necessary Langtrace SDK and OpenAI libraries for Python and TypeScript. ```python pip install langtrace-python-sdk openai ``` ```javascript npm i @langtrase/typescript-sdk openai ``` -------------------------------- ### Setup Environment Variables Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/langchain Sets up essential environment variables for Langtrace and OpenAI API keys, which are required for authentication and operation. ```shell export LANGTRACE_API_KEY=YOUR_LANGTRACE_API_KEY export OPENAI_API_KEY=YOUR_OPENAI_API_KEY ``` -------------------------------- ### Setup RAG Chain and Query Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/langchain Defines a function to format documents, sets up a Retrieval-Augmented Generation (RAG) chain, and invokes it with a query. ```python def format_docs(docs): return "\n\n".join(doc.page_content for doc in docs) rag_chain = ( {"context": retriever | format_docs, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() ) rag_chain.invoke("What is Offside??") ``` -------------------------------- ### Langtrace Azure Setup Configuration Variables Source: https://docs.langtrace.ai/hosting/hosting_options/azure Example JSON structure for configuring deployment variables for Langtrace on Azure. This file, typically named 'main.tfvars.json', allows customization of region, admin credentials, and database settings. ```json { "admin_email": "your_admin_email@example.com", "admin_password": "your_strong_password", "region": "eastus", "postgres_password": "your_postgres_password" } ``` -------------------------------- ### Search Helm Chart Versions Source: https://docs.langtrace.ai/hosting/hosting_options/kubernetes Searches the Helm repository for available versions of the Langtrace chart. ```bash helm search repo langtrace ``` -------------------------------- ### Langtrace Helm Chart Configuration Options Source: https://docs.langtrace.ai/hosting/hosting_options/kubernetes Demonstrates key configuration parameters within the `values.yaml` file for the Langtrace Helm chart, including image repository, tag, and replica count. ```yaml langtraceApp: image: scale3labs/langtrace-client langtrace_release: latest replicaCount: 1 ``` -------------------------------- ### Install Langtrace SDK and Agno Library Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/agno Installs the necessary Python packages for Langtrace and Agno. Ensure you have Python and pip installed. ```bash pip install langtrace-python-sdk pip install agno ``` -------------------------------- ### Clone Langtrace Azure Setup Repository Source: https://docs.langtrace.ai/hosting/hosting_options/azure Clones the official langtrace-azure-setup repository from GitHub and navigates into the cloned directory. This is the first step in setting up Langtrace on Azure. ```bash git clone https://github.com/Scale3-Labs/langtrace-azure-setup.git cd langtrace-azure-setup ``` -------------------------------- ### Upgrade Langtrace Helm Chart Source: https://docs.langtrace.ai/hosting/hosting_options/kubernetes Upgrades an existing Langtrace installation on Kubernetes to the latest version available in the Helm repository. ```bash helm upgrade langtrace langtrace/langtrace ``` -------------------------------- ### cURL Request Example for Get Prompt Source: https://docs.langtrace.ai/api-reference/prompt-registry/GET-prompt-registry Example of how to call the Get Prompt from Registry endpoint using cURL, including authentication and query parameters. ```bash curl --request GET \ --url https://app.langtrace.ai/api/promptset?promptsetid=clw123ay10001v55p02lo1lmp&variables.name=langtrace&variables.org=chatbot \ --header 'x-api-key: 5b4077b5068e79e9b2742fc65afa03327210c4c6f54d213b09f25b516d07ee9f' ``` -------------------------------- ### Python Request Example for Get Prompt Source: https://docs.langtrace.ai/api-reference/prompt-registry/GET-prompt-registry Example of how to call the Get Prompt from Registry endpoint using Python's requests library, demonstrating parameter and header configuration. ```Python import requests url = "https://app.langtrace.ai/api/promptset" params = { "promptsetid": "clw123ay10001v55p02lo1lmp", "variables.name": "langtrace", "variables.org": "chatbot" } headers = { "x-api-key": "5b4077b5068e79e9b2742fc65afa03327210c4c6f54d213b09f25b516d07ee9f" } response = requests.get(url, headers=headers, params=params) print(response.text) ``` -------------------------------- ### JavaScript Request Example for Get Prompt Source: https://docs.langtrace.ai/api-reference/prompt-registry/GET-prompt-registry Example of how to call the Get Prompt from Registry endpoint using JavaScript's fetch API, showing how to set up URL parameters and headers. ```Javascript const url = "https://app.langtrace.ai/api/promptset"; const params = new URLSearchParams({ promptsetid: "clw123ay10001v55p02lo1lmp", "variables.name": "langtrace", "variables.org": "chatbot" }); const headers = { "x-api-key": "5b4077b5068e79e9b2742fc65afa03327210c4c6f54d213b09f25b516d07ee9f" }; fetch(`${url}?${params}`, { method: 'GET', headers: headers }) ``` -------------------------------- ### Running the Scripts Source: https://docs.langtrace.ai/hosting/using_local_setup Instructions on how to set the OpenAI API key environment variable and run the sample Python and Node.js scripts. ```python export OPENAI_API_KEY="" python main.py ``` ```javascript export OPENAI_API_KEY="" node main.js ``` -------------------------------- ### Deploy Langtrace on Azure Source: https://docs.langtrace.ai/hosting/hosting_options/azure Initiates the deployment process for Langtrace on Azure using the Azure Developer CLI. This command provisions all necessary resources and deploys the application. ```bash azd up ``` -------------------------------- ### Install Langtrace and Graphlit SDKs Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/graphlit Installs the necessary Python SDKs for Langtrace and Graphlit using pip. ```bash # Install the SDK pip install langtrace-python-sdk graphlit-client ``` -------------------------------- ### Clone Langtrace Repository Source: https://docs.langtrace.ai/hosting/hosting_options/docker-standalone Clones the Langtrace GitHub repository and navigates into the cloned directory. This is the initial step for setting up the project locally. ```bash git clone https://github.com/Scale3-Labs/langtrace cd langtrace ``` -------------------------------- ### Install OpenTelemetry HTTP/JSON Exporter Source: https://docs.langtrace.ai/supported-integrations/otel-support/ts-http-json Installs the necessary OpenTelemetry package for exporting traces via HTTP/JSON. ```bash npm install @opentelemetry/exporter-trace-otlp-http ``` -------------------------------- ### Create Project using Go Source: https://docs.langtrace.ai/api-reference/project/POST-project Example of creating a project using Go's net/http package. ```go package main import ( "bytes" "encoding/json" "fmt" "net/http" ) func main() { url := "https://app.langtrace.ai/api/project" payload := map[string]interface{}{ "name": "My Project", "description": "Your Project Description", "teamId": "clxw064em0004dpukky3d55lw", "createDefaultTests": true, } jsonData, err := json.Marshal(payload) if err != nil { fmt.Println("Error marshalling JSON:", err) return } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Println("Error creating request:", err) return } req.Header.Set("Content-Type", "application/json") req.Header.Set("x-api-key", "6bd254a6615f48ea0da16823520db8efa4caa4186f62385fbd0f03324c7edcb5") client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error sending request:", err) return } defer resp.Body.Close() fmt.Println("Status Code:", resp.StatusCode) // You can read the response body here if needed } ``` -------------------------------- ### Sample Code for Testing Source: https://docs.langtrace.ai/hosting/using_local_setup A ready-to-run code snippet demonstrating the integration of Langtrace SDK with OpenAI for tracing API calls in Python and TypeScript. ```python from langtrace_python_sdk import langtrace from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span from openai import OpenAI langtrace.init( api_key="", api_host="http://localhost:3000/api/trace", ) @with_langtrace_root_span() def example(): client = OpenAI() response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": "How many states of matter are there?" } ], ) print(response.choices[0].message.content) example() ``` ```javascript import * as Langtrace from "@langtrase/typescript-sdk"; import OpenAI from "openai"; Langtrace.init({ api_key: "", batch: false, api_host: "http://localhost:3000/api/trace", instrumentations: { openai: OpenAI, }, }); const openai = new OpenAI(); async function example() { const completion = await openai.chat.completions.create({ model: "gpt-3.5-turbo", messages: [ { role: "system", content: "How many states of matter are there?", }, ], }); console.log(completion.choices[0]); } example().then(() => { console.log("done"); }); ``` -------------------------------- ### Python SDK Configuration Source: https://docs.langtrace.ai/quickstart Configure the Langtrace Python SDK with options for batching, API key, console output, custom remote exporters, API host, service name, and disabling instrumentations. ```APIDOC Python SDK Configuration: batch: bool (default: True) - Whether to batch spans before sending them. api_key: str (default: LANGTRACE_API_KEY or None) - The API key for authentication. write_spans_to_console: bool (default: False) - Whether to write spans to the console. custom_remote_exporter: Optional[Exporter] (default: None) - Custom remote exporter. If None, a default LangTraceExporter will be used. api_host: Optional[str] (default: https://langtrace.ai/) - The API host for the remote exporter. service_name: Optional[str] (default: None) - The Service name for initializing langtrace. disable_instrumentations: Optional[DisableInstrumentations] (default: None) - Disable instrumentation for specific vendors, e.g., {'only': ['openai']} or {'all_except': ['openai']}. ``` -------------------------------- ### Initialize Langtrace SDK Source: https://docs.langtrace.ai/quickstart Initializes the Langtrace SDK with an API key for Langtrace Cloud integration in TypeScript and Python. ```typescript // Import it into your project. Must precede any llm module imports import * as Langtrace from '@langtrase/typescript-sdk' Langtrace.init({ api_key: '' }) ``` ```python # Import it into your project from langtrace_python_sdk import langtrace # Must precede any llm module imports langtrace.init(api_key = '') ``` -------------------------------- ### Create Project API Key Request Examples Source: https://docs.langtrace.ai/api-reference/project/POST-project-api-key Examples of making a POST request to create a project API key using cURL, Python, JavaScript, PHP, Go, and Java. These examples demonstrate how to set the URL, headers, and send the request. ```bash curl --request POST \ --url https://app.langtrace.ai/api/api-key?project_id=cm0u2v1620001fmtvf2t8kd07 \ --header 'Content-Type: application/json' \ --header 'x-api-key: 6bd254a6615f48ea0da16823520db8efa4caa4186f62385fbd0f03324c7edcb5' ``` ```python import requests url = 'https://app.langtrace.ai/api/api-key?project_id=cm0u2v1620001fmtvf2t8kd07' headers = { 'Content-Type': 'application/json', 'x-api-key': '6bd254a6615f48ea0da16823520db8efa4caa4186f62385fbd0f03324c7edcb5' } response = requests.post(url, headers=headers) print(response.text) ``` ```javascript const url = "https://app.langtrace.ai/api/api-key?project_id=cm0u2v1620001fmtvf2t8kd07"; const headers = { "Content-Type": "application/json", "x-api-key": "6bd254a6615f48ea0da16823520db8efa4caa4186f62385fbd0f03324c7edcb5" }; fetch(url, { method: 'POST', headers: headers, }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error)); ``` ```php "https://app.langtrace.ai/api/api-key?project_id=cm0u2v1620001fmtvf2t8kd07", CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_HTTPHEADER => [ "Content-Type: application/json", "x-api-key: 6bd254a6615f48ea0da16823520db8efa4caa4186f62385fbd0f03324c7edcb5" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } ?> ``` ```go package main import ( "bytes" "encoding/json" "fmt" "net/http" "io/ioutil" ) func main() { url := "https://app.langtrace.ai/api/api-key?project_id=cm0u2v1620001fmtvf2t8kd07" // Assuming payload is defined elsewhere, e.g., var payload map[string]interface{} // For this example, we'll use an empty payload as the API doesn't seem to require one in the body for this endpoint. payload := map[string]interface{}{} jsonData, err := json.Marshal(payload) if err != nil { fmt.Println("Error marshalling JSON:", err) return } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Println("Error creating request:", err) return } req.Header.Set("Content-Type", "application/json") req.Header.Set("x-api-key", "6bd254a6615f48ea0da16823520db8efa4caa4186f62385fbd0f03324c7edcb5") client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error making request:", err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response body:", err) return } fmt.Println(string(body)) } ``` ```java import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.io.IOException; public class Main { public static void main(String[] args) { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://app.langtrace.ai/api/api-key?project_id=cm0u2v1620001fmtvf2t8kd07")) .header("Content-Type", "application/json") .header("x-api-key", "6bd254a6615f48ea0da16823520db8efa4caa4186f62385fbd0f03324c7edcb5") .POST(HttpRequest.BodyPublishers.noBody()) // Assuming no request body is needed for this POST .build(); try { HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println("Status Code: " + response.statusCode()); System.out.println("Response Body: " + response.body()); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` -------------------------------- ### Langtrace Configuration Options Source: https://docs.langtrace.ai/quickstart Details the configuration options available for initializing Langtrace, including API host, instrumentation management, service naming, and disabling specific tracing features. These options are crucial for setting up remote exporters and customizing tracing behavior. ```APIDOC api_host: string The API host for the remote exporter. For self-hosted setups, the URL needs to be appended with `/api/trace`. Default: `https://langtrace.ai/` instrumentations: { [key in InstrumentationType]?: any } This is a required option for Next.js applications. It is used to enable or disable instrumentations. Example: `instrumentations: {openai: openai}` where the value is `import * as openai from 'openai'`. Default: `undefined` service_name: Optional[str] The Service name for initializing Langtrace. Can also be set via `OTEL_SERVICE_NAME` environment variable. Default: `undefined` disable_instrumentations: {all_except?: InstrumentationType[], only?: InstrumentationType[]} You can pass an object to disable instrumentation for specific vendors, e.g., `{'only': ['openai']}` or `{'all_except': ['openai']}`. Default: `{}` disable_tracing_for_methods: Partial You can pass an object to disable tracing for specific methods. Example: `disable_tracing_for_methods: { openai: ['openai.chat.completion'] }`. Full list of methods can be found [here](). Default: `undefined` disable_latest_version_check: boolean Disable the latest version check. This disables the warning when the SDK version is outdated. Default: `false` ``` -------------------------------- ### Uninstall Langtrace Source: https://docs.langtrace.ai/hosting/hosting_options/kubernetes Uninstalls the Langtrace application from Kubernetes. ```bash helm uninstall langtrace ``` -------------------------------- ### Install and Initialize Langtrace SDK Source: https://docs.langtrace.ai/tracing/send_traces Installs the Langtrace SDK and initializes it with your API key. This must be done before importing any LLM modules to ensure proper tracing. ```python # Install the SDK pip install langtrace-python-sdk # Import it into your project from langtrace_python_sdk import langtrace # Must precede any llm module imports langtrace.init(api_key = '') ``` ```typescript // Install the SDK npm i @langtrase/typescript-sdk // Import it into your project. Must precede any llm module imports import * as Langtrace from '@langtrase/typescript-sdk' Langtrace.init({ api_key: ''}) ``` -------------------------------- ### Run OpenTelemetry Collector Source: https://docs.langtrace.ai/supported-integrations/observability-tools/datadog Starts the OpenTelemetry collector with a specified configuration file. ```bash otelcol-contrib --config otel-collector-config.yaml ``` -------------------------------- ### Initialize Qdrant and Langtrace Clients Source: https://docs.langtrace.ai/supported-integrations/vector-stores/qdrant Initializes the Qdrant client connecting to a local instance and initializes the Langtrace SDK with an API key. ```python from langtrace_python_sdk import langtrace, with_langtrace_root_span from qdrant_client import QdrantClient # Initialize Qdrant client qdrant_client = QdrantClient("localhost", port=6333) # Initialize Langtrace langtrace.init(api_key='your_langtrace_api_key_here') ``` -------------------------------- ### Langtrace Python SDK Example Source: https://docs.langtrace.ai/supported-integrations/llm-tools/kubeai A Python script demonstrating how to initialize the Langtrace SDK and send traces for an OpenAI chat completion request via KubeAI. ```python # Replace this with your langtrace API key by visiting http://localhost:3000 langtrace_api_key="f7e003de19b9a628258531c17c264002e985604ca9fa561debcc85c41f357b09" from langtrace_python_sdk import langtrace from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span # Paste this code after your langtrace init function from openai import OpenAI langtrace.init( api_key=api_key, api_host="http://localhost:3000/api/trace", ) base_url = "http://localhost:8000/openai/v1" model = "gemma2-2b-cpu" @with_langtrace_root_span() def example(): client = OpenAI(base_url=base_url, api_key="ignored-by-kubeai") response = client.chat.completions.create( model=model, messages=[ { "role": "system", "content": "How many states of matter are there?" } ], ) print(response.choices[0].message.content) example() ``` -------------------------------- ### Langtrace with ChromaDB Usage Example Source: https://docs.langtrace.ai/supported-integrations/vector-stores/chromadb Demonstrates how to initialize Langtrace, create a ChromaDB client, get or create a collection with an embedding function, add documents, and query the collection. This example highlights the integration of Langtrace's tracing capabilities with ChromaDB operations. ```typescript import * as Langtrace from "@langtrase/typescript-sdk"; import { ChromaClient, OpenAIEmbeddingFunction } from "chromadb"; Langtrace.init({ write_spans_to_console: true }); export async function run(): Promise { await Langtrace.withLangTraceRootSpan(async () => { const client = new ChromaClient(); const embedder = new OpenAIEmbeddingFunction({ openai_api_key: process.env.OPENAI_API_KEY as string, }); console.info("Creating collection"); const collection = await client.getOrCreateCollection({ name: "test_collection", embeddingFunction: embedder, }); console.info("Adding documents"); await collection.add({ ids: ["id1", "id2"], metadatas: [{ source: "my_source" }, { source: "my_source" }], documents: ["This is a document", "This is another document"], }); console.info("Querying documents"); const results = await collection.query({ nResults: 2, queryTexts: ["This is a query document"], }); console.info(results); }); } void run().then(() => console.log("done")); ``` -------------------------------- ### Typescript SDK Configuration Source: https://docs.langtrace.ai/quickstart Configure the Langtrace Typescript SDK with options for API key, batching, console output, and custom remote exporters. ```APIDOC Typescript SDK Configuration: api_key: string (default: LANGTRACE_API_KEY or None) - The API key for authentication. batch: boolean (default: false) - Whether to batch spans before sending them. write_spans_to_console: boolean (default: false) - Whether to write spans to the console. custom_remote_exporter: SpanExporter (default: undefined) - Custom remote exporter. If undefined, a default LangTraceExporter will be used. ``` -------------------------------- ### Setup Environment Variables Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/agno Sets up essential environment variables for Langtrace and the inference provider (e.g., OpenAI). Replace placeholders with your actual API keys. ```bash export LANGTRACE_API_KEY=YOUR_LANGTRACE_API_KEY export OPENAI_API_KEY=YOUR_OPENAI_API_KEY ``` -------------------------------- ### Langtrace AI API Response Examples Source: https://docs.langtrace.ai/api-reference/prompt-registry/GET-prompt-registry Illustrates sample JSON responses from the Langtrace AI API for a successful GET request (200 OK) and an error scenario (e.g., invalid API key). ```APIDOC Sample 200 (OK) Response: { "id": "clw123ay10001v55p02lo1lmp", "name": "Slack Chatbot ", "description": "slack chatbot", "projectId": "clw12223e0001eapng5a66f2u", "createdAt": "2024-05-10T19:14:20.324Z", "updatedAt": "2024-05-10T19:14:20.324Z", "prompts": [ { "id": "clw129y460001o8cgv2uksm3p", "value": "You are a slack chatbot for company langtrace and org chatbot", "variables": [ "name", "org" ], "model": "gpt-3.5-turbo", "modelSettings": {}, "version": 2, "live": true, "tags": [], "spanId": null, "note": "Slack chatbot prompt testing", "promptsetId": "clw123ay10001v55p02lo1lmp", "createdAt": "2024-05-10T19:19:30.293Z", "updatedAt": "2024-05-10T19:19:30.293Z" } ] } Sample Error Response: { "message": "Invalid api key unauthorized", } ``` -------------------------------- ### Setup Graphlit Environment Variables Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/graphlit Configures essential environment variables for connecting to the Graphlit platform with Langtrace. ```shell export LANGTRACE_API_KEY=YOUR_LANGTRACE_API_KEY export GRAPHLIT_ORGANIZATION_ID=YOUR_GRAPHLIT_ORGANIZATION_ID export GRAPHLIT_ENVIRONMENT_ID=YOUR_GRAPHLIT_ENVIRONMENT_ID export GRAPHLIT_JWT_SECRET=YOUR_GRAPHLIT_JWT_SECRET ``` -------------------------------- ### Cleanlab TLM and Langtrace Integration Example Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/cleanlab Demonstrates a complete Python example of integrating Cleanlab TLM with Langtrace for LLM observability. It initializes Langtrace, sets up the Cleanlab TLM client, defines an inference function, and a traced function to get trustworthiness scores. ```python import os from dotenv import find_dotenv, load_dotenv from langtrace_python_sdk import langtrace from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span _ = load_dotenv(find_dotenv()) langtrace.init() from cleanlab_tlm import TLM from openai import OpenAI openai_client = OpenAI() tlm = TLM( api_key=os.getenv("TLM_API_KEY"), options={"log": ["explanation"], "model": "gpt-4o-mini"}, ) def inference(prompt: str): response = openai_client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": prompt}], stream=False, ) response_text = response.choices[0].message.content return response_text @with_langtrace_root_span("Get Trustworthiness Score") def inference_get_trustworthiness_score(prompt: str): response = inference(prompt) return tlm.get_trustworthiness_score(prompt, response) print(inference_get_trustworthiness_score("How many r's are in strawberry?")) ``` -------------------------------- ### Langtrace Client Configuration Recommendations Source: https://docs.langtrace.ai/hosting/recommended_configurations Recommended hardware specifications for the Langtrace Client, a Next.js application serving as the frontend. Includes memory and CPU requirements. ```text Memory: 3GB CPU: 1 vCPU ``` -------------------------------- ### Initialize Langtrace and SwarmZero Agent Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/swarmzero Initializes the Langtrace SDK with an API key and loads environment variables. Then, creates and runs a SwarmZero agent using a specified configuration file. ```python from langtrace_python_sdk import langtrace from swarmzero import Agent from dotenv import load_dotenv import os # Initialize Langtrace langtrace.init(api_key=os.environ['LANGTRACE_API_KEY']) # Load environment variables load_dotenv() # Create and run your agent my_agent = Agent( name="my_agent", functions=[], instruction="your instructions for this agent's goal", config_path="./swarmzero_config.toml" ) my_agent.run() ``` -------------------------------- ### Fetch Promptset Data (Go) Source: https://docs.langtrace.ai/api-reference/prompt-registry/GET-prompt-registry Retrieves promptset data from the Langtrace AI API using Go's net/http package. This example demonstrates making a GET request with an API key and printing the response body or any encountered errors. ```go package main import ( "fmt" "io/ioutil" "net/http" ) func main() { url := "https://app.langtrace.ai/api/promptset?promptsetid=clw123ay10001v55p02lo1lmp&variables.name=langtrace&variables.org=chatbot" req, _ := http.NewRequest("GET", url, nil) req.Header.Add("x-api-key", "5b4077b5068e79e9b2742fc65afa03327210c4c6f54d213b09f25b516d07ee9f") client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error:", err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response body:", err) return } fmt.Println(string(body)) } ``` -------------------------------- ### Clone Langtrace Repository Source: https://docs.langtrace.ai/hosting/hosting_options/docker-compose Clones the Langtrace GitHub repository and navigates into the project directory. This is the initial step for setting up Langtrace locally. ```bash git clone https://github.com/Scale3-Labs/langtrace cd langtrace ``` -------------------------------- ### Ollama Integration Example Source: https://docs.langtrace.ai/supported-integrations/llm-tools/ollama This snippet demonstrates how to use Langtrace with Ollama for monitoring LLM interactions. It assumes the Langtrace SDK has been installed and initialized. ```python # Assuming Langtrace SDK is installed and initialized # from langtrace.trace import trace # from langtrace.langtrace_client import LangTraceClient # Example usage with Ollama (conceptual) # @trace # def ollama_chat(prompt): # # Replace with your actual Ollama interaction logic # response = "This is a simulated Ollama response." # return response # print(ollama_chat("What is Langtrace?")) ``` -------------------------------- ### Langtrace Self-Hosting Instructions Source: https://github.com/Scale3-Labs/langtrace Instructions for self-hosting Langtrace in your own cloud environment. This guide covers the setup process for running Langtrace on your infrastructure. ```APIDOC Self-Hosting Overview: Follow the self hosting setup instructions in our [documentation](https://docs.langtrace.ai/hosting/overview). ``` -------------------------------- ### Instrument ChromaDB with Langtrace Source: https://docs.langtrace.ai/supported-integrations/web-frameworks/nextjs Initializes Langtrace and instruments ChromaDB. Requires ChromaDB installation and OPENAI_API_KEY and LANGTRACE_API_KEY environment variables. Includes setup instructions. ```typescript import * as Langtrace from "@langtrase/typescript-sdk"; import * as chroma from "chromadb"; import * as openai from "openai"; /** * Ensure you have installed chromadb * Make sure you have pipx installed. * python3 -m venv ./app/api/chroma/chromaenv * source ./app/api/chroma/chromaenv/bin/activate * pipx install chromadb * chroma run --path ./app/api/chroma/db * Ensure you have OPENAI_API_KEY in your environment variables and LANGTRACE_API_KEY in your environment variables */ Langtrace.init({ api_key: '', instrumentations: { openai: openai, chromadb: chroma, }, }); ``` -------------------------------- ### Initialize Langtrace SDK Source: https://docs.langtrace.ai/hosting/using_local_setup Initializes the Langtrace SDK with the provided API key and API host for both Python and TypeScript applications. ```python from langtrace_python_sdk import langtrace langtrace.init( api_key="", api_host="http://localhost:3000/api/trace", ) ``` ```javascript import * as Langtrace from "@langtrase/typescript-sdk"; Langtrace.init({ api_key: "", batch: false, api_host: "http://localhost:3000/api/trace", }); ``` -------------------------------- ### Setup Langtrace API Key for LiteLLM SDK Source: https://docs.langtrace.ai/supported-integrations/llm-frameworks/litellm Sets the LANGTRACE_API_KEY environment variable, which is required for Langtrace to authenticate and collect data from LiteLLM. ```bash export LANGTRACE_API_KEY=YOUR_LANGTRACE_API_KEY ```