Skip to content

add llm client option for AI SDK experimental telemetry config #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ObserveOptions,
ObserveResult,
} from "../types/stagehand";
import { AgentExecuteOptions, AgentResult } from ".";
import { AgentExecuteOptions, AgentResult } from "../types/agent";
import {
StagehandAPIUnauthorizedError,
StagehandHttpError,
Expand Down
1 change: 1 addition & 0 deletions lib/llm/LLMProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export class LLMProvider {
logger: this.logger,
enableCaching: this.enableCaching,
cache: this.cache,
telemetrySettings: clientOptions?.aiSdkTelemetrySettings,
});
}

Expand Down
7 changes: 7 additions & 0 deletions lib/llm/aisdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,35 @@ import { LogLine } from "../../types/log";
import { AvailableModel } from "../../types/model";
import { ChatCompletion } from "openai/resources";
import { LLMCache } from "../cache/LLMCache";
import type { TelemetrySettings } from "ai";

export class AISdkClient extends LLMClient {
public type = "aisdk" as const;
private model: LanguageModel;
private logger?: (message: LogLine) => void;
private cache: LLMCache | undefined;
private enableCaching: boolean;
private telemetrySettings: TelemetrySettings | undefined;

constructor({
model,
logger,
enableCaching = false,
cache,
telemetrySettings,
}: {
model: LanguageModel;
logger?: (message: LogLine) => void;
enableCaching?: boolean;
cache?: LLMCache;
telemetrySettings?: TelemetrySettings;
}) {
super(model.modelId as AvailableModel);
this.model = model;
this.logger = logger;
this.cache = cache;
this.enableCaching = enableCaching;
this.telemetrySettings = telemetrySettings;
}

async createChatCompletion<T = ChatCompletion>({
Expand Down Expand Up @@ -161,6 +166,7 @@ export class AISdkClient extends LLMClient {
model: this.model,
messages: formattedMessages,
schema: options.response_model.schema,
experimental_telemetry: this.telemetrySettings,
});

const result = {
Expand Down Expand Up @@ -227,6 +233,7 @@ export class AISdkClient extends LLMClient {
model: this.model,
messages: formattedMessages,
tools,
experimental_telemetry: this.telemetrySettings,
});

const result = {
Expand Down
5 changes: 4 additions & 1 deletion types/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ClientOptions as AnthropicClientOptions } from "@anthropic-ai/sdk";
import type { TelemetrySettings } from "ai";
import type { ClientOptions as OpenAIClientOptions } from "openai";
import { z } from "zod";

Expand Down Expand Up @@ -44,7 +45,9 @@ export type ModelProvider =
| "google"
| "aisdk";

export type ClientOptions = OpenAIClientOptions | AnthropicClientOptions;
export type ClientOptions = (OpenAIClientOptions | AnthropicClientOptions) & {
aiSdkTelemetrySettings?: TelemetrySettings;
};
Comment on lines +48 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider making this a discriminated union with a provider field to ensure type safety when using provider-specific options


export interface AnthropicJsonSchemaObject {
definitions?: {
Expand Down