Skip to content

Commit

Permalink
Remove absolute import paths, update uuid imports (langchain-ai#1142)
Browse files Browse the repository at this point in the history
* Remove absolute import paths

* Fix abs import

* Update imports of uuid to be compatible with misconfigured environments
  • Loading branch information
nfcampos authored May 6, 2023
1 parent 13a37bc commit a049663
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 43 deletions.
4 changes: 2 additions & 2 deletions langchain/src/callbacks/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 as uuidv4 } from "uuid";
import * as uuid from "uuid";
import {
AgentAction,
AgentFinish,
Expand Down Expand Up @@ -179,7 +179,7 @@ export abstract class BaseCallbackHandler

static fromMethods(methods: CallbackHandlerMethods) {
class Handler extends BaseCallbackHandler {
name = uuidv4();
name = uuid.v4();

constructor() {
super();
Expand Down
4 changes: 2 additions & 2 deletions langchain/src/callbacks/tests/callbacks.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from "@jest/globals";
import { v4 as uuidv4 } from "uuid";
import * as uuid from "uuid";
import { CallbackManager } from "../manager.js";
import { BaseCallbackHandler, BaseCallbackHandlerInput } from "../base.js";
import {
Expand All @@ -10,7 +10,7 @@ import {
} from "../../schema/index.js";

class FakeCallbackHandler extends BaseCallbackHandler {
name = `fake-${uuidv4()}`;
name = `fake-${uuid.v4()}`;

starts = 0;

Expand Down
12 changes: 6 additions & 6 deletions langchain/src/callbacks/tests/langchain_tracer.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { v4 as uuidv4 } from "uuid";
/* eslint-disable no-process-env */
import * as uuid from "uuid";
import { test } from "@jest/globals";

import { LangChainTracer } from "../handlers/tracers.js";
Expand All @@ -10,15 +10,15 @@ import { initializeAgentExecutorWithOptions } from "../../agents/index.js";

test("Test LangChain tracer", async () => {
const tracer = new LangChainTracer();
const chainRunId = uuidv4();
const toolRunId = uuidv4();
const llmRunId = uuidv4();
const chainRunId = uuid.v4();
const toolRunId = uuid.v4();
const llmRunId = uuid.v4();
await tracer.handleChainStart({ name: "test" }, { foo: "bar" }, chainRunId);
await tracer.handleToolStart({ name: "test" }, "test", toolRunId, chainRunId);
await tracer.handleLLMStart({ name: "test" }, ["test"], llmRunId, toolRunId);
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId);
await tracer.handleToolEnd("output", toolRunId);
const llmRunId2 = uuidv4();
const llmRunId2 = uuid.v4();
await tracer.handleLLMStart(
{ name: "test2" },
["test"],
Expand All @@ -28,7 +28,7 @@ test("Test LangChain tracer", async () => {
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId2);
await tracer.handleChainEnd({ foo: "bar" }, chainRunId);

const llmRunId3 = uuidv4();
const llmRunId3 = uuid.v4();
await tracer.handleLLMStart({ name: "test" }, ["test"], llmRunId3);
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId3);
});
Expand Down
20 changes: 10 additions & 10 deletions langchain/src/callbacks/tests/tracer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect, jest } from "@jest/globals";
import { v4 as uuidv4 } from "uuid";
import * as uuid from "uuid";
import {
BaseTracer,
LLMRun,
Expand Down Expand Up @@ -57,7 +57,7 @@ class FakeTracer extends BaseTracer {
test("Test LLMRun", async () => {
const tracer = new FakeTracer();
await tracer.newSession();
const runId = uuidv4();
const runId = uuid.v4();
await tracer.handleLLMStart({ name: "test" }, ["test"], runId);
await tracer.handleLLMEnd({ generations: [] }, runId);
expect(tracer.runs.length).toBe(1);
Expand All @@ -80,7 +80,7 @@ test("Test LLMRun", async () => {
test("Test LLM Run no start", async () => {
const tracer = new FakeTracer();
await tracer.newSession();
const runId = uuidv4();
const runId = uuid.v4();
await expect(tracer.handleLLMEnd({ generations: [] }, runId)).rejects.toThrow(
"No LLM run to end"
);
Expand All @@ -89,7 +89,7 @@ test("Test LLM Run no start", async () => {
test("Test Chain Run", async () => {
const tracer = new FakeTracer();
await tracer.newSession();
const runId = uuidv4();
const runId = uuid.v4();
const compareRun: ChainRun = {
uuid: runId,
start_time: _DATE,
Expand All @@ -115,7 +115,7 @@ test("Test Chain Run", async () => {
test("Test Tool Run", async () => {
const tracer = new FakeTracer();
await tracer.newSession();
const runId = uuidv4();
const runId = uuid.v4();
const compareRun: ToolRun = {
uuid: runId,
start_time: _DATE,
Expand All @@ -142,15 +142,15 @@ test("Test Tool Run", async () => {
test("Test nested runs", async () => {
const tracer = new FakeTracer();
await tracer.newSession();
const chainRunId = uuidv4();
const toolRunId = uuidv4();
const llmRunId = uuidv4();
const chainRunId = uuid.v4();
const toolRunId = uuid.v4();
const llmRunId = uuid.v4();
await tracer.handleChainStart({ name: "test" }, { foo: "bar" }, chainRunId);
await tracer.handleToolStart({ name: "test" }, "test", toolRunId, chainRunId);
await tracer.handleLLMStart({ name: "test" }, ["test"], llmRunId, toolRunId);
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId);
await tracer.handleToolEnd("output", toolRunId);
const llmRunId2 = uuidv4();
const llmRunId2 = uuid.v4();
await tracer.handleLLMStart(
{ name: "test2" },
["test"],
Expand Down Expand Up @@ -239,7 +239,7 @@ test("Test nested runs", async () => {
expect(tracer.runs.length).toBe(1);
expect(tracer.runs[0]).toEqual(compareRun);

const llmRunId3 = uuidv4();
const llmRunId3 = uuid.v4();
await tracer.handleLLMStart({ name: "test" }, ["test"], llmRunId3);
await tracer.handleLLMEnd({ generations: [[]] }, llmRunId3);
expect(tracer.runs.length).toBe(2);
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/chat_models/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
AzureOpenAIInput,
OpenAICallOptions,
OpenAIChatInput,
} from "types/open-ai-types.js";
} from "../types/open-ai-types.js";
import fetchAdapter from "../util/axios-fetch-adapter.js";
import type { StreamingAxiosConfiguration } from "../util/axios-types.js";
import { BaseChatModel, BaseChatModelParams } from "./base.js";
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/llms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
AzureOpenAIInput,
OpenAICallOptions,
OpenAIInput,
} from "types/open-ai-types.js";
} from "../types/open-ai-types.js";
import type { StreamingAxiosConfiguration } from "../util/axios-types.js";
import fetchAdapter from "../util/axios-fetch-adapter.js";
import { chunkArray } from "../util/chunk.js";
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/retrievers/time_weighted.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VectorStore } from "vectorstores/base.js";
import { VectorStore } from "../vectorstores/base.js";
import { Document } from "../document.js";
import { BaseRetriever } from "../schema/index.js";

Expand Down
4 changes: 2 additions & 2 deletions langchain/src/vectorstores/chroma.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 as uuidv4 } from "uuid";
import * as uuid from "uuid";
import type { ChromaClient as ChromaClientT, Collection } from "chromadb";

import { Embeddings } from "../embeddings/base.js";
Expand Down Expand Up @@ -182,7 +182,7 @@ export class Chroma extends VectorStore {

function ensureCollectionName(collectionName?: string) {
if (!collectionName) {
return `langchain-${uuidv4()}`;
return `langchain-${uuid.v4()}`;
}
return collectionName;
}
4 changes: 2 additions & 2 deletions langchain/src/vectorstores/milvus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 as uuidv4 } from "uuid";
import * as uuid from "uuid";
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
import {
DataType,
Expand Down Expand Up @@ -487,7 +487,7 @@ function createFieldTypeForMetadata(documents: Document[]): FieldType[] {
}

function genCollectionName(): string {
return `${MILVUS_COLLECTION_NAME_PREFIX}_${uuidv4().replaceAll("-", "")}`;
return `${MILVUS_COLLECTION_NAME_PREFIX}_${uuid.v4().replaceAll("-", "")}`;
}

function getTextFieldMaxLength(documents: Document[]) {
Expand Down
6 changes: 3 additions & 3 deletions langchain/src/vectorstores/myscale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 as uuid } from "uuid";
import * as uuid from "uuid";
import { ClickHouseClient, createClient } from "@clickhouse/client";

import { Embeddings } from "../embeddings/base.js";
Expand Down Expand Up @@ -70,7 +70,7 @@ export class MyScaleStore extends VectorStore {
host: `${args.protocol ?? "https://"}${args.host}:${args.port}`,
username: args.username,
password: args.password,
session_id: uuid(),
session_id: uuid.v4(),
});
}

Expand Down Expand Up @@ -190,7 +190,7 @@ export class MyScaleStore extends VectorStore {
const vector = vectors[i];
const document = documents[i];
const item = [
`'${uuid()}'`,
`'${uuid.v4()}'`,
`'${this.escapeString(document.pageContent)}'`,
`[${vector}]`,
`'${JSON.stringify(document.metadata)}'`,
Expand Down
4 changes: 2 additions & 2 deletions langchain/src/vectorstores/opensearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client, RequestParams, errors } from "@opensearch-project/opensearch";
import { v4 as uuid } from "uuid";
import * as uuid from "uuid";
import { Embeddings } from "../embeddings/base.js";
import { Document } from "../document.js";
import { VectorStore } from "./base.js";
Expand Down Expand Up @@ -75,7 +75,7 @@ export class OpenSearchVectorStore extends VectorStore {
{
index: {
_index: this.indexName,
_id: uuid(),
_id: uuid.v4(),
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions langchain/src/vectorstores/pinecone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 as uuidv4 } from "uuid";
import * as uuid from "uuid";
import flatten from "flat";

import { VectorStore } from "./base.js";
Expand Down Expand Up @@ -54,7 +54,7 @@ export class PineconeStore extends VectorStore {
documents: Document[],
ids?: string[]
): Promise<void> {
const documentIds = ids == null ? documents.map(() => uuidv4()) : ids;
const documentIds = ids == null ? documents.map(() => uuid.v4()) : ids;
const pineconeVectors = vectors.map((values, idx) => {
// Pinecone doesn't support nested objects, so we flatten them
const metadata: {
Expand Down
12 changes: 6 additions & 6 deletions langchain/src/vectorstores/tests/pinecone.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { beforeEach, describe, expect, test } from "@jest/globals";
import { faker } from "@faker-js/faker";
import { PineconeClient } from "@pinecone-database/pinecone";
import { v4 as uuidv4 } from "uuid";
import * as uuid from "uuid";
import { Document } from "../../document.js";
import { OpenAIEmbeddings } from "../../embeddings/openai.js";
import { PineconeStore } from "../pinecone.js";
Expand All @@ -25,7 +25,7 @@ describe("PineconeStore", () => {
});

test("user-provided ids", async () => {
const documentId = uuidv4();
const documentId = uuid.v4();
const pageContent = faker.lorem.sentence(5);

await pineconeStore.addDocuments(
Expand Down Expand Up @@ -54,21 +54,21 @@ describe("PineconeStore", () => {

test("metadata filtering", async () => {
const pageContent = faker.lorem.sentence(5);
const uuid = uuidv4();
const id = uuid.v4();

await pineconeStore.addDocuments([
{ pageContent, metadata: { foo: "bar" } },
{ pageContent, metadata: { foo: uuid } },
{ pageContent, metadata: { foo: id } },
{ pageContent, metadata: { foo: "qux" } },
]);

// If the filter wasn't working, we'd get all 3 documents back
const results = await pineconeStore.similaritySearch(pageContent, 3, {
foo: uuid,
foo: id,
});

expect(results).toEqual([
new Document({ metadata: { foo: uuid }, pageContent }),
new Document({ metadata: { foo: id }, pageContent }),
]);
});
});
4 changes: 2 additions & 2 deletions langchain/src/vectorstores/weaviate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 } from "uuid";
import * as uuid from "uuid";
import type {
WeaviateObject,
WeaviateClient,
Expand Down Expand Up @@ -103,7 +103,7 @@ export class WeaviateStore extends VectorStore {
const flattenedMetadata = flattenObjectForWeaviate(document.metadata);
return {
class: this.indexName,
id: v4(),
id: uuid.v4(),
vector: vectors[index],
properties: {
[this.textKey]: document.pageContent,
Expand Down
1 change: 0 additions & 1 deletion langchain/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"module": "ES2020",
"moduleResolution": "nodenext",
"esModuleInterop": true,
"baseUrl": "./src",
"declaration": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
Expand Down
3 changes: 3 additions & 0 deletions test-exports-esm/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { HNSWLib } from "langchain/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { InMemoryDocstore, Document } from "langchain/docstore";
import { CSVLoader } from "langchain/document_loaders/fs/csv";
import { CallbackManager } from "langchain/callbacks";

// Test exports
assert(typeof OpenAI === "function");
assert(typeof LLMChain === "function");
assert(typeof loadPrompt === "function");
assert(typeof ChatPromptTemplate === "function");
assert(typeof HNSWLib === "function");
assert(typeof OpenAIEmbeddings === "function");
assert(typeof CallbackManager === "function");

// Test dynamic imports of peer dependencies
const { HierarchicalNSW } = await HNSWLib.imports();
Expand Down

0 comments on commit a049663

Please sign in to comment.