diff --git a/.changeset/chilled-dogs-compare.md b/.changeset/chilled-dogs-compare.md new file mode 100644 index 000000000..11af5d227 --- /dev/null +++ b/.changeset/chilled-dogs-compare.md @@ -0,0 +1,5 @@ +--- +"create-llama": patch +--- + +Add support for LlamaTrace (Python) diff --git a/create-app.ts b/create-app.ts index 008ab91ba..7e00e0bab 100644 --- a/create-app.ts +++ b/create-app.ts @@ -9,7 +9,7 @@ import { makeDir } from "./helpers/make-dir"; import fs from "fs"; import terminalLink from "terminal-link"; -import type { InstallTemplateArgs } from "./helpers"; +import type { InstallTemplateArgs, TemplateObservability } from "./helpers"; import { installTemplate } from "./helpers"; import { writeDevcontainer } from "./helpers/devcontainer"; import { templatesDir } from "./helpers/dir"; @@ -142,14 +142,7 @@ export async function createApp({ )} and learn how to get started.`, ); - if (args.observability === "opentelemetry") { - console.log( - `\n${yellow("Observability")}: Visit the ${terminalLink( - "documentation", - "https://traceloop.com/docs/openllmetry/integrations", - )} to set up the environment variables and start seeing execution traces.`, - ); - } + outputObservability(args.observability); if ( dataSources.some((dataSource) => dataSource.type === "file") && @@ -167,3 +160,24 @@ export async function createApp({ console.log(); } + +function outputObservability(observability?: TemplateObservability) { + switch (observability) { + case "traceloop": + console.log( + `\n${yellow("Observability")}: Visit the ${terminalLink( + "documentation", + "https://traceloop.com/docs/openllmetry/integrations", + )} to set up the environment variables and start seeing execution traces.`, + ); + break; + case "llamatrace": + console.log( + `\n${yellow("Observability")}: LlamaTrace has been configured for your project. Visit the ${terminalLink( + "LlamaTrace dashboard", + "https://llamatrace.com/login", + )} to view your traces and monitor your application.`, + ); + break; + } +} diff --git a/helpers/env-variables.ts b/helpers/env-variables.ts index 34930cad5..553b96536 100644 --- a/helpers/env-variables.ts +++ b/helpers/env-variables.ts @@ -2,9 +2,10 @@ import fs from "fs/promises"; import path from "path"; import { TOOL_SYSTEM_PROMPT_ENV_VAR, Tool } from "./tools"; import { + InstallTemplateArgs, ModelConfig, - TemplateDataSource, TemplateFramework, + TemplateObservability, TemplateType, TemplateVectorDB, } from "./types"; @@ -461,18 +462,35 @@ const getTemplateEnvs = (template?: TemplateType): EnvVar[] => { } }; +const getObservabilityEnvs = ( + observability?: TemplateObservability, +): EnvVar[] => { + if (observability === "llamatrace") { + return [ + { + name: "PHOENIX_API_KEY", + description: + "API key for LlamaTrace observability. Retrieve from https://llamatrace.com/login", + }, + ]; + } + return []; +}; + export const createBackendEnvFile = async ( root: string, - opts: { - llamaCloudKey?: string; - vectorDb?: TemplateVectorDB; - modelConfig: ModelConfig; - framework: TemplateFramework; - dataSources?: TemplateDataSource[]; - template?: TemplateType; - port?: number; - tools?: Tool[]; - }, + opts: Pick< + InstallTemplateArgs, + | "llamaCloudKey" + | "vectorDb" + | "modelConfig" + | "framework" + | "dataSources" + | "template" + | "externalPort" + | "tools" + | "observability" + >, ) => { // Init env values const envFileName = ".env"; @@ -482,16 +500,14 @@ export const createBackendEnvFile = async ( description: `The Llama Cloud API key.`, value: opts.llamaCloudKey, }, - // Add model environment variables + // Add environment variables of each component ...getModelEnvs(opts.modelConfig), - // Add engine environment variables ...getEngineEnvs(), - // Add vector database environment variables ...getVectorDBEnvs(opts.vectorDb, opts.framework), - ...getFrameworkEnvs(opts.framework, opts.port), + ...getFrameworkEnvs(opts.framework, opts.externalPort), ...getToolEnvs(opts.tools), - // Add template environment variables ...getTemplateEnvs(opts.template), + ...getObservabilityEnvs(opts.observability), getSystemPromptEnv(opts.tools), ]; // Render and write env file diff --git a/helpers/index.ts b/helpers/index.ts index 79f3c6873..91eed694d 100644 --- a/helpers/index.ts +++ b/helpers/index.ts @@ -168,16 +168,7 @@ export const installTemplate = async ( props.template === "multiagent" || props.template === "extractor" ) { - await createBackendEnvFile(props.root, { - modelConfig: props.modelConfig, - llamaCloudKey: props.llamaCloudKey, - vectorDb: props.vectorDb, - framework: props.framework, - dataSources: props.dataSources, - port: props.externalPort, - tools: props.tools, - template: props.template, - }); + await createBackendEnvFile(props.root, props); } if (props.dataSources.length > 0) { diff --git a/helpers/python.ts b/helpers/python.ts index 095c1a0a2..6bb9dc902 100644 --- a/helpers/python.ts +++ b/helpers/python.ts @@ -380,18 +380,27 @@ export const installPythonTemplate = async ({ tools, ); - if (observability === "opentelemetry") { - addOnDependencies.push({ - name: "traceloop-sdk", - version: "^0.15.11", - }); + if (observability && observability !== "none") { + if (observability === "traceloop") { + addOnDependencies.push({ + name: "traceloop-sdk", + version: "^0.15.11", + }); + } + + if (observability === "llamatrace") { + addOnDependencies.push({ + name: "llama-index-callbacks-arize-phoenix", + version: "^0.1.6", + }); + } const templateObservabilityPath = path.join( templatesDir, "components", "observability", "python", - "opentelemetry", + observability, ); await copy("**", path.join(root, "app"), { cwd: templateObservabilityPath, diff --git a/helpers/types.ts b/helpers/types.ts index 9dc9686de..0e4fde4d3 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -46,7 +46,7 @@ export type TemplateDataSource = { config: TemplateDataSourceConfig; }; export type TemplateDataSourceType = "file" | "web" | "db" | "llamacloud"; -export type TemplateObservability = "none" | "opentelemetry"; +export type TemplateObservability = "none" | "traceloop" | "llamatrace"; // Config for both file and folder export type FileSourceConfig = { path: string; diff --git a/helpers/typescript.ts b/helpers/typescript.ts index def104da4..7becd8e7e 100644 --- a/helpers/typescript.ts +++ b/helpers/typescript.ts @@ -70,7 +70,7 @@ export const installTSTemplate = async ({ ); const webpackConfigOtelFile = path.join(root, "webpack.config.o11y.mjs"); - if (observability === "opentelemetry") { + if (observability === "traceloop") { const webpackConfigDefaultFile = path.join(root, "webpack.config.mjs"); await fs.rm(webpackConfigDefaultFile); await fs.rename(webpackConfigOtelFile, webpackConfigDefaultFile); @@ -248,7 +248,7 @@ async function updatePackageJson({ }; } - if (observability === "opentelemetry") { + if (observability === "traceloop") { packageJson.dependencies = { ...packageJson.dependencies, "@traceloop/node-server-sdk": "^0.5.19", diff --git a/questions.ts b/questions.ts index 6141d99fc..6d42fd229 100644 --- a/questions.ts +++ b/questions.ts @@ -486,7 +486,10 @@ export const askQuestions = async ( message: "Would you like to set up observability?", choices: [ { title: "No", value: "none" }, - { title: "OpenTelemetry", value: "opentelemetry" }, + ...(program.framework === "fastapi" + ? [{ title: "LlamaTrace", value: "llamatrace" }] + : []), + { title: "Traceloop", value: "traceloop" }, ], initial: 0, }, diff --git a/templates/components/observability/python/llamatrace/observability.py b/templates/components/observability/python/llamatrace/observability.py new file mode 100644 index 000000000..9bd415844 --- /dev/null +++ b/templates/components/observability/python/llamatrace/observability.py @@ -0,0 +1,12 @@ +import llama_index.core +import os + + +def init_observability(): + PHOENIX_API_KEY = os.getenv("PHOENIX_API_KEY") + if not PHOENIX_API_KEY: + raise ValueError("PHOENIX_API_KEY environment variable is not set") + os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"api_key={PHOENIX_API_KEY}" + llama_index.core.set_global_handler( + "arize_phoenix", endpoint="https://llamatrace.com/v1/traces" + ) diff --git a/templates/components/observability/python/opentelemetry/observability.py b/templates/components/observability/python/traceloop/observability.py similarity index 100% rename from templates/components/observability/python/opentelemetry/observability.py rename to templates/components/observability/python/traceloop/observability.py diff --git a/templates/components/observability/typescript/opentelemetry/index.ts b/templates/components/observability/typescript/traceloop/index.ts similarity index 100% rename from templates/components/observability/typescript/opentelemetry/index.ts rename to templates/components/observability/typescript/traceloop/index.ts