Skip to content

Commit d9d9fba

Browse files
authored
[workchat] add tracing configuration (#217825)
## Summary Allow enabling langsmith tracing via kibana config file ### Example ```yaml xpack.workchatApp.tracing.langsmith: enabled: true apiKey: {API-KEY} project: {project-name} ```
1 parent 3068c83 commit d9d9fba

File tree

9 files changed

+115
-2
lines changed

9 files changed

+115
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
# WorkChatApp
22

33
WorkChat application plugin
4+
5+
6+
## Enabling tracing
7+
8+
### Langsmith tracing
9+
10+
Enabling langsmith tracing can be done by adding the corresponding entry to your Kibana dev config file:
11+
12+
```yaml
13+
xpack.workchatApp.tracing.langsmith:
14+
enabled: true
15+
apiKey: {API-KEY}
16+
project: {project-name}
17+
```

x-pack/solutions/chat/plugins/workchat-app/server/config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,25 @@ import { PluginConfigDescriptor } from '@kbn/core/server';
1010

1111
const configSchema = schema.object({
1212
enabled: schema.boolean({ defaultValue: false }),
13+
tracing: schema.object(
14+
{
15+
langsmith: schema.maybe(
16+
schema.object({
17+
enabled: schema.boolean({ defaultValue: false }),
18+
apiKey: schema.string(),
19+
apiUrl: schema.string({ defaultValue: 'https://api.smith.langchain.com' }),
20+
project: schema.string(),
21+
})
22+
),
23+
},
24+
{ defaultValue: {} }
25+
),
1326
});
1427

1528
export type WorkChatAppConfig = TypeOf<typeof configSchema>;
1629

30+
export type WorkChatTracingConfig = WorkChatAppConfig['tracing'];
31+
1732
export const config: PluginConfigDescriptor<WorkChatAppConfig> = {
1833
exposeToBrowser: {},
1934
schema: configSchema,

x-pack/solutions/chat/plugins/workchat-app/server/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { registerFeatures } from './features';
1818
import type { InternalServices } from './services/types';
1919
import { IntegrationRegistry } from './services/integrations';
2020
import { createServices } from './services/create_services';
21+
import type { WorkChatAppConfig } from './config';
2122
import type {
2223
WorkChatAppPluginSetup,
2324
WorkChatAppPluginStart,
@@ -35,11 +36,13 @@ export class WorkChatAppPlugin
3536
>
3637
{
3738
private readonly logger: LoggerFactory;
39+
private readonly config: WorkChatAppConfig;
3840
private readonly integrationRegistry = new IntegrationRegistry();
3941
private services?: InternalServices;
4042

4143
constructor(context: PluginInitializerContext) {
4244
this.logger = context.logger;
45+
this.config = context.config.get<WorkChatAppConfig>();
4346
}
4447

4548
public setup(
@@ -78,6 +81,7 @@ export class WorkChatAppPlugin
7881
): WorkChatAppPluginStart {
7982
this.services = createServices({
8083
core,
84+
config: this.config,
8185
logger: this.logger,
8286
pluginsDependencies,
8387
integrationRegistry: this.integrationRegistry,

x-pack/solutions/chat/plugins/workchat-app/server/services/create_services.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import type { CoreStart } from '@kbn/core/server';
99
import type { LoggerFactory } from '@kbn/core/server';
1010
import type { WorkChatAppPluginStartDependencies } from '../types';
11+
import type { WorkChatAppConfig } from '../config';
1112
import type { InternalServices } from './types';
1213
import { IntegrationsServiceImpl } from './integrations/integrations_service';
1314
import { ConversationServiceImpl } from './conversations';
@@ -18,13 +19,15 @@ import { IntegrationRegistry } from './integrations';
1819

1920
interface CreateServicesParams {
2021
core: CoreStart;
22+
config: WorkChatAppConfig;
2123
logger: LoggerFactory;
2224
pluginsDependencies: WorkChatAppPluginStartDependencies;
2325
integrationRegistry: IntegrationRegistry;
2426
}
2527

2628
export function createServices({
2729
core,
30+
config,
2831
logger,
2932
pluginsDependencies,
3033
integrationRegistry,
@@ -53,6 +56,7 @@ export function createServices({
5356

5457
const agentFactory = new AgentFactory({
5558
inference: pluginsDependencies.inference,
59+
tracingConfig: config.tracing,
5660
logger: logger.get('services.agentFactory'),
5761
agentService,
5862
integrationsService,

x-pack/solutions/chat/plugins/workchat-app/server/services/orchestration/agent_factory.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import { KibanaRequest, Logger } from '@kbn/core/server';
99
import { InferenceServerStart } from '@kbn/inference-plugin/server';
10+
import type { WorkChatTracingConfig } from '../../config';
1011
import { IntegrationsServiceImpl } from '../integrations/integrations_service';
1112
import type { AgentService } from '../agents';
1213
import type { AgentRunner } from './types';
@@ -19,19 +20,28 @@ interface AgentFactoryArgs {
1920
inference: InferenceServerStart;
2021
agentService: AgentService;
2122
integrationsService: IntegrationsServiceImpl;
23+
tracingConfig: WorkChatTracingConfig;
2224
}
2325

2426
export class AgentFactory {
2527
private readonly inference: InferenceServerStart;
2628
private readonly logger: Logger;
2729
private readonly agentService: AgentService;
2830
private readonly integrationsService: IntegrationsServiceImpl;
31+
private readonly tracingConfig: WorkChatTracingConfig;
2932

30-
constructor({ inference, logger, integrationsService, agentService }: AgentFactoryArgs) {
33+
constructor({
34+
inference,
35+
logger,
36+
integrationsService,
37+
agentService,
38+
tracingConfig,
39+
}: AgentFactoryArgs) {
3140
this.inference = inference;
3241
this.logger = logger;
3342
this.integrationsService = integrationsService;
3443
this.agentService = agentService;
44+
this.tracingConfig = tracingConfig;
3545
}
3646

3747
async getAgentRunner({
@@ -61,7 +71,13 @@ export class AgentFactory {
6171
chatModelOptions: {},
6272
});
6373

64-
return await createAgentRunner({ agent, chatModel, createSession, logger: this.logger });
74+
return await createAgentRunner({
75+
agent,
76+
chatModel,
77+
createSession,
78+
logger: this.logger,
79+
tracingConfig: this.tracingConfig,
80+
});
6581
}
6682

6783
private async createGatewaySession({

x-pack/solutions/chat/plugins/workchat-app/server/services/orchestration/agent_runner.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,31 @@ import { StreamEvent } from '@langchain/core/tracers/log_stream';
1010
import type { Logger } from '@kbn/core/server';
1111
import type { InferenceChatModel } from '@kbn/inference-langchain';
1212
import type { Agent } from '../../../common/agents';
13+
import type { WorkChatTracingConfig } from '../../config';
1314
import { createAgentGraph } from './agent_graph';
1415
import { conversationEventsToMessages } from './utils';
1516
import { convertGraphEvents } from './graph_events';
1617
import type { AgentRunner, AgentRunResult } from './types';
1718
import type { McpGatewaySession } from './mcp_gateway';
1819
import { graphNames } from './constants';
1920
import { getGraphMeta } from './graph_events';
21+
import { getTracers } from './tracing';
2022

2123
export const createAgentRunner = async ({
2224
logger,
2325
agent,
2426
chatModel,
2527
createSession,
28+
tracingConfig,
2629
}: {
2730
logger: Logger;
2831
agent: Agent;
2932
chatModel: InferenceChatModel;
3033
createSession: () => Promise<McpGatewaySession>;
34+
tracingConfig: WorkChatTracingConfig;
3135
}): Promise<AgentRunner> => {
3236
const session = await createSession();
37+
const tracers = getTracers({ config: tracingConfig });
3338

3439
const closeSession = () => {
3540
session.close().catch((err) => {
@@ -55,6 +60,7 @@ export const createAgentRunner = async ({
5560
agentId: agent.id,
5661
},
5762
recursionLimit: 10,
63+
callbacks: [...tracers],
5864
}
5965
);
6066

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { LangChainTracer } from '@langchain/core/tracers/tracer_langchain';
9+
import type { WorkChatTracingConfig } from '../../../config';
10+
import { getLangsmithTracer } from './langsmith';
11+
12+
export const getTracers = ({ config }: { config: WorkChatTracingConfig }) => {
13+
const tracers: LangChainTracer[] = [];
14+
if (config?.langsmith?.enabled) {
15+
tracers.push(
16+
getLangsmithTracer({
17+
apiKey: config.langsmith.apiKey,
18+
apiUrl: config.langsmith.apiUrl,
19+
project: config.langsmith.project,
20+
})
21+
);
22+
}
23+
return tracers;
24+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
export { getTracers } from './get_tracers';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { Client } from 'langsmith';
9+
import { LangChainTracer } from '@langchain/core/tracers/tracer_langchain';
10+
11+
export interface LangsmithTracingConfig {
12+
apiKey: string;
13+
apiUrl: string;
14+
project: string;
15+
}
16+
17+
export const getLangsmithTracer = (config: LangsmithTracingConfig): LangChainTracer => {
18+
return new LangChainTracer({
19+
projectName: config.project,
20+
client: new Client({ apiKey: config.apiKey, apiUrl: config.apiUrl }),
21+
});
22+
};

0 commit comments

Comments
 (0)