langsmith 0.0.35 → 0.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,7 +26,7 @@ services:
26
26
  environment:
27
27
  - LANGCHAIN_ENV=local_docker
28
28
  - LOG_LEVEL=warning
29
- entrypoint: "rq worker --with-scheduler -u redis://langchain-redis:6379 --serializer lc_database.queue.serializer.ORJSONSerializer --worker-class lc_database.queue.worker.Worker --connection-class lc_database.queue.connection.RedisRetry"
29
+ entrypoint: "rq worker --with-scheduler -u redis://langchain-redis:6379 --serializer lc_database.queue.serializer.ORJSONSerializer --worker-class lc_database.queue.worker.Worker --connection-class lc_database.queue.connection.RedisRetry --job-class lc_database.queue.job.AsyncJob"
30
30
  langchain-hub:
31
31
  image: langchain/${_LANGSMITH_IMAGE_PREFIX-}langchainhub-backend:latest
32
32
  environment:
package/dist/client.cjs CHANGED
@@ -26,6 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.Client = void 0;
27
27
  const uuid = __importStar(require("uuid"));
28
28
  const async_caller_js_1 = require("./utils/async_caller.cjs");
29
+ const messages_js_1 = require("./utils/messages.cjs");
29
30
  const env_js_1 = require("./utils/env.cjs");
30
31
  // utility functions
31
32
  const isLocalhost = (url) => {
@@ -580,7 +581,7 @@ class Client {
580
581
  }
581
582
  await response.json();
582
583
  }
583
- async createExample(inputs, outputs, { datasetId, datasetName, createdAt, }) {
584
+ async createExample(inputs, outputs, { datasetId, datasetName, createdAt, exampleId }) {
584
585
  let datasetId_ = datasetId;
585
586
  if (datasetId_ === undefined && datasetName === undefined) {
586
587
  throw new Error("Must provide either datasetName or datasetId");
@@ -598,6 +599,7 @@ class Client {
598
599
  inputs,
599
600
  outputs,
600
601
  created_at: createdAt_.toISOString(),
602
+ id: exampleId,
601
603
  };
602
604
  const response = await this.caller.call(fetch, `${this.apiUrl}/examples`, {
603
605
  method: "POST",
@@ -611,6 +613,21 @@ class Client {
611
613
  const result = await response.json();
612
614
  return result;
613
615
  }
616
+ async createLLMExample(input, generation, options) {
617
+ return this.createExample({ input }, { output: generation }, options);
618
+ }
619
+ async createChatExample(input, generations, options) {
620
+ const finalInput = input.map((message) => {
621
+ if ((0, messages_js_1.isLangChainMessage)(message)) {
622
+ return (0, messages_js_1.convertLangChainMessageToExample)(message);
623
+ }
624
+ return message;
625
+ });
626
+ const finalOutput = (0, messages_js_1.isLangChainMessage)(generations)
627
+ ? (0, messages_js_1.convertLangChainMessageToExample)(generations)
628
+ : generations;
629
+ return this.createExample({ input: finalInput }, { output: finalOutput }, options);
630
+ }
614
631
  async readExample(exampleId) {
615
632
  const path = `/examples/${exampleId}`;
616
633
  return await this._get(path);
@@ -695,7 +712,7 @@ class Client {
695
712
  feedbackSourceType: "model",
696
713
  });
697
714
  }
698
- async createFeedback(runId, key, { score, value, correction, comment, sourceInfo, feedbackSourceType = "api", sourceRunId, }) {
715
+ async createFeedback(runId, key, { score, value, correction, comment, sourceInfo, feedbackSourceType = "api", sourceRunId, feedbackId, }) {
699
716
  const feedback_source = {
700
717
  type: feedbackSourceType ?? "api",
701
718
  metadata: sourceInfo ?? {},
@@ -706,7 +723,7 @@ class Client {
706
723
  feedback_source.metadata["__run"] = { run_id: sourceRunId };
707
724
  }
708
725
  const feedback = {
709
- id: uuid.v4(),
726
+ id: feedbackId ?? uuid.v4(),
710
727
  run_id: runId,
711
728
  key,
712
729
  score,
@@ -721,11 +738,8 @@ class Client {
721
738
  body: JSON.stringify(feedback),
722
739
  signal: AbortSignal.timeout(this.timeout_ms),
723
740
  });
724
- if (!response.ok) {
725
- throw new Error(`Failed to create feedback for run ${runId}: ${response.status} ${response.statusText}`);
726
- }
727
- const result = await response.json();
728
- return result;
741
+ await raiseForStatus(response, "create feedback");
742
+ return feedback;
729
743
  }
730
744
  async updateFeedback(feedbackId, { score, value, correction, comment, }) {
731
745
  const feedbackUpdate = {};
package/dist/client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AsyncCallerParams } from "./utils/async_caller.js";
2
- import { Dataset, Example, ExampleUpdate, Feedback, KVMap, Run, RunCreate, RunUpdate, ScoreType, TracerSession, TracerSessionResult, ValueType, DataType } from "./schemas.js";
2
+ import { Dataset, Example, ExampleUpdate, Feedback, KVMap, Run, RunCreate, RunUpdate, ScoreType, TracerSession, TracerSessionResult, ValueType, DataType, LangChainBaseMessage } from "./schemas.js";
3
3
  import { RunEvaluator } from "./evaluation/evaluator.js";
4
4
  interface ClientConfig {
5
5
  apiUrl?: string;
@@ -49,6 +49,12 @@ interface CreateRunParams {
49
49
  project_name?: string;
50
50
  }
51
51
  export type FeedbackSourceType = "model" | "api" | "app";
52
+ export type CreateExampleOptions = {
53
+ datasetId?: string;
54
+ datasetName?: string;
55
+ createdAt?: Date;
56
+ exampleId?: string;
57
+ };
52
58
  export declare class Client {
53
59
  private apiKey?;
54
60
  private apiUrl;
@@ -120,11 +126,9 @@ export declare class Client {
120
126
  datasetId?: string;
121
127
  datasetName?: string;
122
128
  }): Promise<void>;
123
- createExample(inputs: KVMap, outputs: KVMap, { datasetId, datasetName, createdAt, }: {
124
- datasetId?: string;
125
- datasetName?: string;
126
- createdAt?: Date;
127
- }): Promise<Example>;
129
+ createExample(inputs: KVMap, outputs: KVMap, { datasetId, datasetName, createdAt, exampleId }: CreateExampleOptions): Promise<Example>;
130
+ createLLMExample(input: string, generation: string | undefined, options: CreateExampleOptions): Promise<Example>;
131
+ createChatExample(input: KVMap[] | LangChainBaseMessage[], generations: KVMap | LangChainBaseMessage | undefined, options: CreateExampleOptions): Promise<Example>;
128
132
  readExample(exampleId: string): Promise<Example>;
129
133
  listExamples({ datasetId, datasetName, exampleIds, }?: {
130
134
  datasetId?: string;
@@ -137,7 +141,7 @@ export declare class Client {
137
141
  sourceInfo?: KVMap;
138
142
  loadChildRuns: boolean;
139
143
  }): Promise<Feedback>;
140
- createFeedback(runId: string, key: string, { score, value, correction, comment, sourceInfo, feedbackSourceType, sourceRunId, }: {
144
+ createFeedback(runId: string, key: string, { score, value, correction, comment, sourceInfo, feedbackSourceType, sourceRunId, feedbackId, }: {
141
145
  score?: ScoreType;
142
146
  value?: ValueType;
143
147
  correction?: object;
@@ -145,6 +149,7 @@ export declare class Client {
145
149
  sourceInfo?: object;
146
150
  feedbackSourceType?: FeedbackSourceType;
147
151
  sourceRunId?: string;
152
+ feedbackId?: string;
148
153
  }): Promise<Feedback>;
149
154
  updateFeedback(feedbackId: string, { score, value, correction, comment, }: {
150
155
  score?: number | boolean | null;
package/dist/client.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as uuid from "uuid";
2
2
  import { AsyncCaller } from "./utils/async_caller.js";
3
+ import { convertLangChainMessageToExample, isLangChainMessage, } from "./utils/messages.js";
3
4
  import { getEnvironmentVariable, getRuntimeEnvironment } from "./utils/env.js";
4
5
  // utility functions
5
6
  const isLocalhost = (url) => {
@@ -554,7 +555,7 @@ export class Client {
554
555
  }
555
556
  await response.json();
556
557
  }
557
- async createExample(inputs, outputs, { datasetId, datasetName, createdAt, }) {
558
+ async createExample(inputs, outputs, { datasetId, datasetName, createdAt, exampleId }) {
558
559
  let datasetId_ = datasetId;
559
560
  if (datasetId_ === undefined && datasetName === undefined) {
560
561
  throw new Error("Must provide either datasetName or datasetId");
@@ -572,6 +573,7 @@ export class Client {
572
573
  inputs,
573
574
  outputs,
574
575
  created_at: createdAt_.toISOString(),
576
+ id: exampleId,
575
577
  };
576
578
  const response = await this.caller.call(fetch, `${this.apiUrl}/examples`, {
577
579
  method: "POST",
@@ -585,6 +587,21 @@ export class Client {
585
587
  const result = await response.json();
586
588
  return result;
587
589
  }
590
+ async createLLMExample(input, generation, options) {
591
+ return this.createExample({ input }, { output: generation }, options);
592
+ }
593
+ async createChatExample(input, generations, options) {
594
+ const finalInput = input.map((message) => {
595
+ if (isLangChainMessage(message)) {
596
+ return convertLangChainMessageToExample(message);
597
+ }
598
+ return message;
599
+ });
600
+ const finalOutput = isLangChainMessage(generations)
601
+ ? convertLangChainMessageToExample(generations)
602
+ : generations;
603
+ return this.createExample({ input: finalInput }, { output: finalOutput }, options);
604
+ }
588
605
  async readExample(exampleId) {
589
606
  const path = `/examples/${exampleId}`;
590
607
  return await this._get(path);
@@ -669,7 +686,7 @@ export class Client {
669
686
  feedbackSourceType: "model",
670
687
  });
671
688
  }
672
- async createFeedback(runId, key, { score, value, correction, comment, sourceInfo, feedbackSourceType = "api", sourceRunId, }) {
689
+ async createFeedback(runId, key, { score, value, correction, comment, sourceInfo, feedbackSourceType = "api", sourceRunId, feedbackId, }) {
673
690
  const feedback_source = {
674
691
  type: feedbackSourceType ?? "api",
675
692
  metadata: sourceInfo ?? {},
@@ -680,7 +697,7 @@ export class Client {
680
697
  feedback_source.metadata["__run"] = { run_id: sourceRunId };
681
698
  }
682
699
  const feedback = {
683
- id: uuid.v4(),
700
+ id: feedbackId ?? uuid.v4(),
684
701
  run_id: runId,
685
702
  key,
686
703
  score,
@@ -695,11 +712,8 @@ export class Client {
695
712
  body: JSON.stringify(feedback),
696
713
  signal: AbortSignal.timeout(this.timeout_ms),
697
714
  });
698
- if (!response.ok) {
699
- throw new Error(`Failed to create feedback for run ${runId}: ${response.status} ${response.statusText}`);
700
- }
701
- const result = await response.json();
702
- return result;
715
+ await raiseForStatus(response, "create feedback");
716
+ return feedback;
703
717
  }
704
718
  async updateFeedback(feedbackId, { score, value, correction, comment, }) {
705
719
  const feedbackUpdate = {};
package/dist/schemas.d.ts CHANGED
@@ -164,3 +164,8 @@ export interface FeedbackCreate extends FeedbackBase {
164
164
  export interface Feedback extends FeedbackBase {
165
165
  id: string;
166
166
  }
167
+ export interface LangChainBaseMessage {
168
+ _getType: () => string;
169
+ content: string;
170
+ additional_kwargs?: KVMap;
171
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertLangChainMessageToExample = exports.isLangChainMessage = void 0;
4
+ function isLangChainMessage(message) {
5
+ return typeof message?._getType === "function";
6
+ }
7
+ exports.isLangChainMessage = isLangChainMessage;
8
+ function convertLangChainMessageToExample(message) {
9
+ const converted = {
10
+ type: message._getType(),
11
+ data: { content: message.content },
12
+ };
13
+ // Check for presence of keys in additional_kwargs
14
+ if (message?.additional_kwargs &&
15
+ Object.keys(message.additional_kwargs).length > 0) {
16
+ converted.data.additional_kwargs = { ...message.additional_kwargs };
17
+ }
18
+ return converted;
19
+ }
20
+ exports.convertLangChainMessageToExample = convertLangChainMessageToExample;
@@ -0,0 +1,11 @@
1
+ import { LangChainBaseMessage } from "../schemas.js";
2
+ export declare function isLangChainMessage(message?: any): message is LangChainBaseMessage;
3
+ interface ConvertedData {
4
+ content: string;
5
+ [key: string]: any;
6
+ }
7
+ export declare function convertLangChainMessageToExample(message: LangChainBaseMessage): {
8
+ type: string;
9
+ data: ConvertedData;
10
+ };
11
+ export {};
@@ -0,0 +1,15 @@
1
+ export function isLangChainMessage(message) {
2
+ return typeof message?._getType === "function";
3
+ }
4
+ export function convertLangChainMessageToExample(message) {
5
+ const converted = {
6
+ type: message._getType(),
7
+ data: { content: message.content },
8
+ };
9
+ // Check for presence of keys in additional_kwargs
10
+ if (message?.additional_kwargs &&
11
+ Object.keys(message.additional_kwargs).length > 0) {
12
+ converted.data.additional_kwargs = { ...message.additional_kwargs };
13
+ }
14
+ return converted;
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langsmith",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
4
4
  "description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
5
5
  "files": [
6
6
  "dist/",
@@ -72,6 +72,7 @@
72
72
  "eslint-plugin-no-instanceof": "^1.0.1",
73
73
  "eslint-plugin-prettier": "^4.2.1",
74
74
  "jest": "^29.5.0",
75
+ "langchain": "^0.0.147",
75
76
  "prettier": "^2.8.8",
76
77
  "ts-jest": "^29.1.0",
77
78
  "ts-node": "^10.9.1",