Skip to content

Commit 4b00c7f

Browse files
committed
sync loopback4-llm-chat-extension docs
1 parent 4b0f744 commit 4b00c7f

File tree

1 file changed

+112
-0
lines changed
  • docs/arc-api-docs/extensions/loopback4-llm-chat-extension

1 file changed

+112
-0
lines changed

docs/arc-api-docs/extensions/loopback4-llm-chat-extension/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,118 @@ export class AddTool implements IGraphTool {
492492
}
493493
```
494494

495+
# Observability
496+
497+
## With Langsmith
498+
499+
You can enable langsmith observability by simply adding the Langsmith env variables. Refer [this](https://docs.langchain.com/langsmith/observability-quickstart) for more details.
500+
501+
## With Langfuse
502+
503+
You can enable Langfuse based tracing with the following steps -
504+
505+
- install the following packages -
506+
507+
```sh
508+
npm i @langfuse/core @langfuse/langchain @langfuse/otel
509+
```
510+
511+
- adding the following component in your LB4 application -
512+
513+
```ts
514+
import {LangfuseComponent} from 'lb4-llm-chat-component/langfuse';
515+
...
516+
517+
this.component(LangfuseComponent);
518+
```
519+
520+
- set up langfuseSpanProcessor -
521+
522+
```ts
523+
import {LangfuseSpanProcessor} from '@langfuse/otel';
524+
import {OTLPTraceExporter} from '@opentelemetry/exporter-trace-otlp-http';
525+
import {DnsInstrumentation} from '@opentelemetry/instrumentation-dns';
526+
import {HttpInstrumentation} from '@opentelemetry/instrumentation-http';
527+
import {PgInstrumentation} from '@opentelemetry/instrumentation-pg';
528+
import {
529+
defaultResource,
530+
resourceFromAttributes,
531+
} from '@opentelemetry/resources';
532+
import {NodeSDK} from '@opentelemetry/sdk-node';
533+
import {BatchSpanProcessor, SpanProcessor} from '@opentelemetry/sdk-trace-base';
534+
import {
535+
ATTR_SERVICE_NAME,
536+
ATTR_SERVICE_VERSION,
537+
} from '@opentelemetry/semantic-conventions';
538+
import * as dotenv from 'dotenv';
539+
import * as dotenvExt from 'dotenv-extended';
540+
541+
dotenv.config();
542+
dotenvExt.load({
543+
schema: '.env.example',
544+
errorOnMissing: true,
545+
includeProcessEnv: true,
546+
});
547+
548+
if (!!+(process.env.ENABLE_TRACING ?? 0)) {
549+
const resource = defaultResource().merge(
550+
resourceFromAttributes({
551+
[ATTR_SERVICE_NAME]: process.env.SERVICE_NAME ?? 'reporting-service',
552+
[ATTR_SERVICE_VERSION]: process.env.SERVICE_VERSION ?? '1.0.0',
553+
}),
554+
);
555+
556+
const spanProcessors: SpanProcessor[] = [];
557+
const instrumentations = [];
558+
559+
// Add OTLP exporter if Jaeger endpoint is configured
560+
if (process.env.OPENTELEMETRY_HOST && process.env.OPENTELEMETRY_PORT) {
561+
const otlpExporter = new OTLPTraceExporter({
562+
url: `http://${process.env.OPENTELEMETRY_HOST}:${process.env.OPENTELEMETRY_PORT}/v1/traces`,
563+
});
564+
spanProcessors.push(new BatchSpanProcessor(otlpExporter));
565+
instrumentations.push(
566+
new HttpInstrumentation(),
567+
new DnsInstrumentation(),
568+
new PgInstrumentation(),
569+
);
570+
}
571+
572+
if (process.env.LANGFUSE_BASE_URL) {
573+
console.log('Langfuse tracing enabled');
574+
spanProcessors.push(new LangfuseSpanProcessor());
575+
}
576+
577+
const sdk = new NodeSDK({
578+
resource: resource,
579+
spanProcessors: spanProcessors,
580+
instrumentations,
581+
});
582+
583+
// Initialize the SDK
584+
console.log('Starting OpenTelemetry SDK');
585+
sdk.start();
586+
587+
// Graceful shutdown
588+
process.on('SIGTERM', () => {
589+
sdk
590+
.shutdown()
591+
.then(() => console.log('Tracing terminated'))
592+
.catch(error => console.log('Error terminating tracing', error))
593+
.finally(() => process.exit(0));
594+
});
595+
}
596+
```
597+
598+
- setup env -
599+
600+
```sh
601+
export ENABLE_TRACING=1
602+
export LANGFUSE_SECRET_KEY="sk-lf-..."
603+
export LANGFUSE_PUBLIC_KEY="pk-lf-..."
604+
export LANGFUSE_BASE_URL="https://cloud.langfuse.com"
605+
```
606+
495607
# Testing
496608

497609
## Generation Acceptance Builder

0 commit comments

Comments
 (0)