Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/hwchase17/langchainjs into …
Browse files Browse the repository at this point in the history
…rahilvora/add_agent_scratchpad
  • Loading branch information
jacoblee93 committed Mar 30, 2024
2 parents 8f320d9 + 4a5cd29 commit 534c00b
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@langchain/weaviate": "workspace:*",
"@langchain/yandex": "workspace:*",
"@opensearch-project/opensearch": "^2.2.0",
"@pinecone-database/pinecone": "^2.0.0",
"@pinecone-database/pinecone": "^2.2.0",
"@planetscale/database": "^1.8.0",
"@prisma/client": "^4.11.0",
"@raycast/api": "^1.55.2",
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-openai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/openai",
"version": "0.0.24",
"version": "0.0.25",
"description": "OpenAI integrations for LangChain.js",
"type": "module",
"engines": {
Expand Down
3 changes: 2 additions & 1 deletion libs/langchain-openai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,9 @@ export class ChatOpenAI<
openAIFunctionDefinition = schema as FunctionDefinition;
functionName = schema.name;
} else {
functionName = schema.title ?? functionName;
openAIFunctionDefinition = {
name: schema.title ?? functionName,
name: functionName,
description: schema.description ?? "",
parameters: schema,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,46 @@ Respond with a JSON object containing three keys:
expect("number2" in result).toBe(true);
});

test("withStructuredOutput JSON schema", async () => {
const model = new ChatOpenAI({
temperature: 0,
modelName: "gpt-4-turbo-preview",
});

const jsonSchema = {
title: "calculator",
description: "A simple calculator",
type: "object",
properties: {
operation: {
type: "string",
enum: ["add", "subtract", "multiply", "divide"],
},
number1: { type: "number" },
number2: { type: "number" },
},
};
const modelWithStructuredOutput = model.withStructuredOutput(jsonSchema);

const prompt = ChatPromptTemplate.fromMessages([
"system",
`You are VERY bad at math and must always use a calculator.
Respond with a JSON object containing three keys:
'operation': the type of operation to execute, either 'add', 'subtract', 'multiply' or 'divide',
'number1': the first number to operate on,
'number2': the second number to operate on.
`,
"human",
"Please help me!! What is 2 + 2?",
]);
const chain = prompt.pipe(modelWithStructuredOutput);
const result = await chain.invoke({});
console.log(result);
expect("operation" in result).toBe(true);
expect("number1" in result).toBe(true);
expect("number2" in result).toBe(true);
});

test("withStructuredOutput includeRaw true", async () => {
const model = new ChatOpenAI({
temperature: 0,
Expand Down
4 changes: 2 additions & 2 deletions libs/langchain-pinecone/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/pinecone",
"version": "0.0.3",
"version": "0.0.4",
"description": "LangChain integration for Pinecone's vector database",
"type": "module",
"engines": {
Expand Down Expand Up @@ -39,7 +39,7 @@
"license": "MIT",
"dependencies": {
"@langchain/core": "~0.1",
"@pinecone-database/pinecone": "^2.0.0",
"@pinecone-database/pinecone": "^2.2.0",
"flat": "^5.0.2",
"uuid": "^9.0.0"
},
Expand Down
36 changes: 36 additions & 0 deletions libs/langchain-pinecone/src/tests/vectorstores.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,40 @@ describe.skip("PineconeStore", () => {
expect(results.length).toEqual(1);
expect(results[0].metadata.foo).toBe(id1);
});

test("auto instantiated pinecone index class", async () => {
const documentId = uuid.v4();
const pageContent = faker.lorem.sentence(5);
const embeddings = new SyntheticEmbeddings({
vectorSize: 1536,
});

const store = new PineconeStore(embeddings, {
pineconeConfig: {
indexName: testIndexName,
config: {
apiKey: process.env.PINECONE_API_KEY!,
},
},
});

await store.addDocuments([{ pageContent, metadata: {} }], [documentId]);
await sleep(35000);

const results = await store.similaritySearch(pageContent, 1);

expect(results).toEqual([new Document({ metadata: {}, pageContent })]);

await store.addDocuments(
[{ pageContent: `${pageContent} upserted`, metadata: {} }],
[documentId]
);
await sleep(35000);

const results2 = await store.similaritySearch(pageContent, 1);

expect(results2).toEqual([
new Document({ metadata: {}, pageContent: `${pageContent} upserted` }),
]);
});
});
47 changes: 47 additions & 0 deletions libs/langchain-pinecone/src/tests/vectorstores.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,50 @@ test("PineconeStore with string arrays", async () => {
},
]);
});

test("PineconeStore can instantiate without passing in client", async () => {
const embeddings = new FakeEmbeddings();

const store = new PineconeStore(embeddings, {
pineconeConfig: {
indexName: "indexName",
config: {
apiKey: "apiKey",
},
},
});

expect(store.pineconeIndex).toBeDefined();
});

test("PineconeStore throws when no config or index is passed", async () => {
const embeddings = new FakeEmbeddings();

expect(() => new PineconeStore(embeddings, {})).toThrow();
});

test("PineconeStore throws when config and index is passed", async () => {
const upsert = jest.fn();
const client = {
namespace: jest.fn<any>().mockReturnValue({
upsert,
query: jest.fn<any>().mockResolvedValue({
matches: [],
}),
}),
};
const embeddings = new FakeEmbeddings();

expect(
() =>
new PineconeStore(embeddings, {
pineconeIndex: client as any,
pineconeConfig: {
indexName: "indexName",
config: {
apiKey: "apiKey",
},
},
})
).toThrow();
});
56 changes: 52 additions & 4 deletions libs/langchain-pinecone/src/vectorstores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,33 @@ import { maximalMarginalRelevance } from "@langchain/core/utils/math";
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any
type PineconeMetadata = Record<string, any>;

type HTTPHeaders = {
[key: string]: string;
};

/**
* Database config for your vectorstore.
*/
export interface PineconeStoreParams extends AsyncCallerParams {
pineconeIndex: PineconeIndex;
/**
* The Pinecone index to use.
* Either this or pineconeConfig must be provided.
*/
pineconeIndex?: PineconeIndex;
textKey?: string;
namespace?: string;
filter?: PineconeMetadata;
/**
* Configuration for the Pinecone index.
* Either this or pineconeIndex must be provided.
*/
pineconeConfig?: {
indexName: ConstructorParameters<typeof PineconeIndex>[0];
config: ConstructorParameters<typeof PineconeIndex>[1];
namespace?: string;
indexHostUrl?: string;
additionalHeaders?: HTTPHeaders;
};
}

/**
Expand Down Expand Up @@ -69,10 +88,39 @@ export class PineconeStore extends VectorStore {
super(embeddings, params);
this.embeddings = embeddings;

const { namespace, pineconeIndex, textKey, filter, ...asyncCallerArgs } =
params;
const {
namespace,
pineconeIndex,
textKey,
filter,
pineconeConfig,
...asyncCallerArgs
} = params;
this.namespace = namespace;
this.pineconeIndex = pineconeIndex;
if (!pineconeIndex && !pineconeConfig) {
throw new Error("pineconeConfig or pineconeIndex must be provided.");
}
if (pineconeIndex && pineconeConfig) {
throw new Error(
"Only one of pineconeConfig or pineconeIndex can be provided."
);
}

if (pineconeIndex) {
this.pineconeIndex = pineconeIndex;
} else if (pineconeConfig) {
this.pineconeIndex = new PineconeIndex(
pineconeConfig.indexName,
{
...pineconeConfig.config,
sourceTag: "langchainjs",
},
pineconeConfig.namespace,
pineconeConfig.indexHostUrl,
pineconeConfig.additionalHeaders
);
}

this.textKey = textKey ?? "text";
this.filter = filter;
this.caller = new AsyncCaller(asyncCallerArgs);
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9782,7 +9782,7 @@ __metadata:
"@jest/globals": ^29.5.0
"@langchain/core": ~0.1
"@langchain/scripts": ~0.0
"@pinecone-database/pinecone": ^2.0.0
"@pinecone-database/pinecone": ^2.2.0
"@swc/core": ^1.3.90
"@swc/jest": ^0.2.29
"@tsconfig/recommended": ^1.0.3
Expand Down Expand Up @@ -10747,15 +10747,15 @@ __metadata:
languageName: node
linkType: hard

"@pinecone-database/pinecone@npm:^2.0.0":
version: 2.0.1
resolution: "@pinecone-database/pinecone@npm:2.0.1"
"@pinecone-database/pinecone@npm:^2.2.0":
version: 2.2.0
resolution: "@pinecone-database/pinecone@npm:2.2.0"
dependencies:
"@sinclair/typebox": ^0.29.0
ajv: ^8.12.0
cross-fetch: ^3.1.5
encoding: ^0.1.13
checksum: 43ece04cd66a597281a92d189561bd8eed133a541c32c9d2f8a7c10b2b6c899eeaaf3c7a14387ba62e05f0fa5c441bd3588a9a6c36dddd265699587a9eea8cd1
checksum: c1844eaa716746a3895871499cbf3fa6ecf20deb0b236999d28285cfb9b438fb053fb41a4c9c17f96b28f0dab9c09be59308476a1534d4a8f35518b761a1a148
languageName: node
linkType: hard

Expand Down Expand Up @@ -21646,7 +21646,7 @@ __metadata:
"@langchain/weaviate": "workspace:*"
"@langchain/yandex": "workspace:*"
"@opensearch-project/opensearch": ^2.2.0
"@pinecone-database/pinecone": ^2.0.0
"@pinecone-database/pinecone": ^2.2.0
"@planetscale/database": ^1.8.0
"@prisma/client": ^4.11.0
"@raycast/api": ^1.55.2
Expand Down

0 comments on commit 534c00b

Please sign in to comment.