Skip to content

Commit d2b2eb8

Browse files
feat: add LlamaTrace support
1 parent c06ec4f commit d2b2eb8

File tree

11 files changed

+95
-45
lines changed

11 files changed

+95
-45
lines changed

.changeset/chilled-dogs-compare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-llama": patch
3+
---
4+
5+
Add support for LlamaTrace (Python)

create-app.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { makeDir } from "./helpers/make-dir";
99

1010
import fs from "fs";
1111
import terminalLink from "terminal-link";
12-
import type { InstallTemplateArgs } from "./helpers";
12+
import type { InstallTemplateArgs, TemplateObservability } from "./helpers";
1313
import { installTemplate } from "./helpers";
1414
import { writeDevcontainer } from "./helpers/devcontainer";
1515
import { templatesDir } from "./helpers/dir";
@@ -142,14 +142,7 @@ export async function createApp({
142142
)} and learn how to get started.`,
143143
);
144144

145-
if (args.observability === "opentelemetry") {
146-
console.log(
147-
`\n${yellow("Observability")}: Visit the ${terminalLink(
148-
"documentation",
149-
"https://traceloop.com/docs/openllmetry/integrations",
150-
)} to set up the environment variables and start seeing execution traces.`,
151-
);
152-
}
145+
outputObservability(args.observability);
153146

154147
if (
155148
dataSources.some((dataSource) => dataSource.type === "file") &&
@@ -167,3 +160,24 @@ export async function createApp({
167160

168161
console.log();
169162
}
163+
164+
function outputObservability(observability?: TemplateObservability) {
165+
switch (observability) {
166+
case "traceloop":
167+
console.log(
168+
`\n${yellow("Observability")}: Visit the ${terminalLink(
169+
"documentation",
170+
"https://traceloop.com/docs/openllmetry/integrations",
171+
)} to set up the environment variables and start seeing execution traces.`,
172+
);
173+
break;
174+
case "llamatrace":
175+
console.log(
176+
`\n${yellow("Observability")}: LlamaTrace has been configured for your project. Visit the ${terminalLink(
177+
"LlamaTrace dashboard",
178+
"https://llamatrace.com/login",
179+
)} to view your traces and monitor your application.`,
180+
);
181+
break;
182+
}
183+
}

helpers/env-variables.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import fs from "fs/promises";
22
import path from "path";
33
import { TOOL_SYSTEM_PROMPT_ENV_VAR, Tool } from "./tools";
44
import {
5+
InstallTemplateArgs,
56
ModelConfig,
6-
TemplateDataSource,
77
TemplateFramework,
8+
TemplateObservability,
89
TemplateType,
910
TemplateVectorDB,
1011
} from "./types";
@@ -461,18 +462,35 @@ const getTemplateEnvs = (template?: TemplateType): EnvVar[] => {
461462
}
462463
};
463464

465+
const getObservabilityEnvs = (
466+
observability?: TemplateObservability,
467+
): EnvVar[] => {
468+
if (observability === "llamatrace") {
469+
return [
470+
{
471+
name: "PHOENIX_API_KEY",
472+
description:
473+
"API key for LlamaTrace observability. Retrieve from https://llamatrace.com/login",
474+
},
475+
];
476+
}
477+
return [];
478+
};
479+
464480
export const createBackendEnvFile = async (
465481
root: string,
466-
opts: {
467-
llamaCloudKey?: string;
468-
vectorDb?: TemplateVectorDB;
469-
modelConfig: ModelConfig;
470-
framework: TemplateFramework;
471-
dataSources?: TemplateDataSource[];
472-
template?: TemplateType;
473-
port?: number;
474-
tools?: Tool[];
475-
},
482+
opts: Pick<
483+
InstallTemplateArgs,
484+
| "llamaCloudKey"
485+
| "vectorDb"
486+
| "modelConfig"
487+
| "framework"
488+
| "dataSources"
489+
| "template"
490+
| "externalPort"
491+
| "tools"
492+
| "observability"
493+
>,
476494
) => {
477495
// Init env values
478496
const envFileName = ".env";
@@ -482,16 +500,14 @@ export const createBackendEnvFile = async (
482500
description: `The Llama Cloud API key.`,
483501
value: opts.llamaCloudKey,
484502
},
485-
// Add model environment variables
503+
// Add environment variables of each component
486504
...getModelEnvs(opts.modelConfig),
487-
// Add engine environment variables
488505
...getEngineEnvs(),
489-
// Add vector database environment variables
490506
...getVectorDBEnvs(opts.vectorDb, opts.framework),
491-
...getFrameworkEnvs(opts.framework, opts.port),
507+
...getFrameworkEnvs(opts.framework, opts.externalPort),
492508
...getToolEnvs(opts.tools),
493-
// Add template environment variables
494509
...getTemplateEnvs(opts.template),
510+
...getObservabilityEnvs(opts.observability),
495511
getSystemPromptEnv(opts.tools),
496512
];
497513
// Render and write env file

helpers/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,7 @@ export const installTemplate = async (
168168
props.template === "multiagent" ||
169169
props.template === "extractor"
170170
) {
171-
await createBackendEnvFile(props.root, {
172-
modelConfig: props.modelConfig,
173-
llamaCloudKey: props.llamaCloudKey,
174-
vectorDb: props.vectorDb,
175-
framework: props.framework,
176-
dataSources: props.dataSources,
177-
port: props.externalPort,
178-
tools: props.tools,
179-
template: props.template,
180-
});
171+
await createBackendEnvFile(props.root, props);
181172
}
182173

183174
if (props.dataSources.length > 0) {

helpers/python.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,18 +380,27 @@ export const installPythonTemplate = async ({
380380
tools,
381381
);
382382

383-
if (observability === "opentelemetry") {
384-
addOnDependencies.push({
385-
name: "traceloop-sdk",
386-
version: "^0.15.11",
387-
});
383+
if (observability && observability !== "none") {
384+
if (observability === "traceloop") {
385+
addOnDependencies.push({
386+
name: "traceloop-sdk",
387+
version: "^0.15.11",
388+
});
389+
}
390+
391+
if (observability === "llamatrace") {
392+
addOnDependencies.push({
393+
name: "llama-index-callbacks-arize-phoenix",
394+
version: "^0.1.6",
395+
});
396+
}
388397

389398
const templateObservabilityPath = path.join(
390399
templatesDir,
391400
"components",
392401
"observability",
393402
"python",
394-
"opentelemetry",
403+
observability,
395404
);
396405
await copy("**", path.join(root, "app"), {
397406
cwd: templateObservabilityPath,

helpers/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export type TemplateDataSource = {
4646
config: TemplateDataSourceConfig;
4747
};
4848
export type TemplateDataSourceType = "file" | "web" | "db" | "llamacloud";
49-
export type TemplateObservability = "none" | "opentelemetry";
49+
export type TemplateObservability = "none" | "traceloop" | "llamatrace";
5050
// Config for both file and folder
5151
export type FileSourceConfig = {
5252
path: string;

helpers/typescript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const installTSTemplate = async ({
7070
);
7171

7272
const webpackConfigOtelFile = path.join(root, "webpack.config.o11y.mjs");
73-
if (observability === "opentelemetry") {
73+
if (observability === "traceloop") {
7474
const webpackConfigDefaultFile = path.join(root, "webpack.config.mjs");
7575
await fs.rm(webpackConfigDefaultFile);
7676
await fs.rename(webpackConfigOtelFile, webpackConfigDefaultFile);
@@ -248,7 +248,7 @@ async function updatePackageJson({
248248
};
249249
}
250250

251-
if (observability === "opentelemetry") {
251+
if (observability === "traceloop") {
252252
packageJson.dependencies = {
253253
...packageJson.dependencies,
254254
"@traceloop/node-server-sdk": "^0.5.19",

questions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,10 @@ export const askQuestions = async (
486486
message: "Would you like to set up observability?",
487487
choices: [
488488
{ title: "No", value: "none" },
489-
{ title: "OpenTelemetry", value: "opentelemetry" },
489+
...(program.framework === "fastapi"
490+
? [{ title: "LlamaTrace", value: "llamatrace" }]
491+
: []),
492+
{ title: "Traceloop", value: "traceloop" },
490493
],
491494
initial: 0,
492495
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import llama_index.core
2+
import os
3+
4+
5+
def init_observability():
6+
PHOENIX_API_KEY = os.getenv("PHOENIX_API_KEY")
7+
if not PHOENIX_API_KEY:
8+
raise ValueError("PHOENIX_API_KEY environment variable is not set")
9+
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"api_key={PHOENIX_API_KEY}"
10+
llama_index.core.set_global_handler(
11+
"arize_phoenix", endpoint="https://llamatrace.com/v1/traces"
12+
)

0 commit comments

Comments
 (0)