Skip to content

Commit

Permalink
Merge branch 'main' into fix/6173
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul authored Jul 23, 2024
2 parents ea97e5b + 99a8760 commit 41ce3e1
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 44 deletions.
12 changes: 11 additions & 1 deletion docs/core_docs/docs/how_to/qa_per_user.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,19 @@
"\n",
"const pineconeIndex = pinecone.Index(Deno.env.get(\"PINECONE_INDEX\"));\n",
"\n",
"/**\n",
" * Pinecone allows you to partition the records in an index into namespaces. \n",
" * Queries and other operations are then limited to one namespace, \n",
" * so different requests can search different subsets of your index.\n",
" * Read more about namespaces here: https://docs.pinecone.io/guides/indexes/use-namespaces\n",
" * \n",
" * NOTE: If you have namespace enabled in your Pinecone index, you must provide the namespace when creating the PineconeStore.\n",
" */\n",
"const namespace = \"pinecone\";\n",
"\n",
"const vectorStore = await PineconeStore.fromExistingIndex(\n",
" new OpenAIEmbeddings(),\n",
" { pineconeIndex },\n",
" { pineconeIndex, namespace },\n",
");\n",
"\n",
"await vectorStore.addDocuments(\n",
Expand Down
6 changes: 5 additions & 1 deletion docs/core_docs/docs/integrations/chat/ollama.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import OllamaExample from "@examples/models/chat/integration_ollama.ts";

<CodeBlock language="typescript">{OllamaExample}</CodeBlock>

:::tip
See a LangSmith trace of the above example [here](https://smith.langchain.com/public/c4f3cb1a-4496-40bd-854c-898118b21809/r)
:::

## Tools

Ollama now offers support for native tool calling. The example below demonstrates how you can invoke a tool from an Ollama model.
Expand Down Expand Up @@ -61,7 +65,7 @@ import OllamaJSONModeExample from "@examples/models/chat/integration_ollama_json
<CodeBlock language="typescript">{OllamaJSONModeExample}</CodeBlock>

:::tip
You can see a simple LangSmith trace of this [here](https://smith.langchain.com/public/1fbd5660-b7fd-41c3-9d3a-a6ecc735277c/r)
You can see a simple LangSmith trace of this [here](https://smith.langchain.com/public/54c0430c-0d59-4121-b1ca-34ac1e95e0bb/r)
:::

## Multimodal models
Expand Down
1 change: 1 addition & 0 deletions docs/core_docs/docs/integrations/chat/ollama_functions.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_label: Ollama Functions
sidebar_class_name: hidden
---

# Ollama Functions
Expand Down
12 changes: 11 additions & 1 deletion examples/src/indexes/vector_stores/pinecone/mmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ const pinecone = new Pinecone();

const pineconeIndex = pinecone.Index(process.env.PINECONE_INDEX!);

/**
* Pinecone allows you to partition the records in an index into namespaces.
* Queries and other operations are then limited to one namespace,
* so different requests can search different subsets of your index.
* Read more about namespaces here: https://docs.pinecone.io/guides/indexes/use-namespaces
*
* NOTE: If you have namespace enabled in your Pinecone index, you must provide the namespace when creating the PineconeStore.
*/
const namespace = "pinecone";

const vectorStore = await PineconeStore.fromExistingIndex(
new OpenAIEmbeddings(),
{ pineconeIndex }
{ pineconeIndex, namespace }
);

/* Search the vector DB independently with meta filters */
Expand Down
12 changes: 11 additions & 1 deletion examples/src/indexes/vector_stores/pinecone/query_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ const pinecone = new Pinecone();

const pineconeIndex = pinecone.Index(process.env.PINECONE_INDEX!);

/**
* Pinecone allows you to partition the records in an index into namespaces.
* Queries and other operations are then limited to one namespace,
* so different requests can search different subsets of your index.
* Read more about namespaces here: https://docs.pinecone.io/guides/indexes/use-namespaces
*
* NOTE: If you have namespace enabled in your Pinecone index, you must provide the namespace when creating the PineconeStore.
*/
const namespace = "pinecone";

const vectorStore = await PineconeStore.fromExistingIndex(
new OpenAIEmbeddings(),
{ pineconeIndex }
{ pineconeIndex, namespace }
);

/* Search the vector DB independently with metadata filters */
Expand Down
24 changes: 17 additions & 7 deletions examples/src/models/chat/integration_ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StringOutputParser } from "@langchain/core/output_parsers";

const model = new ChatOllama({
baseUrl: "http://localhost:11434", // Default value
model: "llama2", // Default value
model: "llama3",
});

const stream = await model
Expand All @@ -18,13 +18,23 @@ for await (const chunk of stream) {
console.log(chunks.join(""));

/*
Thank you for your question! I'm happy to help. However, I must point out that the phrase "I love programming" is not grammatically correct in German. The word "love" does not have a direct translation in German, and it would be more appropriate to say "I enjoy programming" or "I am passionate about programming."
The translation of "I love programming" into German is:
In German, you can express your enthusiasm for something like this:
Ich liebe Programmieren.
* Ich möchte Programmieren (I want to program)
* Ich mag Programmieren (I like to program)
* Ich bin passioniert über Programmieren (I am passionate about programming)
Here's a breakdown of the translation:
I hope this helps! Let me know if you have any other questions.
* Ich = I
* liebe = love
* Programmieren = programming (note: this word is the infinitive form, which is often used in informal contexts or when speaking about one's profession or hobby)
If you want to make it sound more formal or use the correct verb conjugation for "I", you can say:
Ich bin ein großer Fan von Programmieren.
This translates to:
I am a big fan of programming.
In this sentence, "bin" is the first person singular present tense of the verb "sein", which means "to be". The phrase "ein großer Fan" means "a big fan".
*/
15 changes: 8 additions & 7 deletions examples/src/models/chat/integration_ollama_json_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const prompt = ChatPromptTemplate.fromMessages([

const model = new ChatOllama({
baseUrl: "http://localhost:11434", // Default value
model: "llama2", // Default value
model: "llama3",
format: "json",
});

Expand All @@ -26,11 +26,12 @@ console.log(result);

/*
AIMessage {
content: '{\n' +
'"original": "I love programming",\n' +
'"translated": "Ich liebe Programmierung"\n' +
'}',
response_metadata: { ... },
usage_metadata: { ... }
"content": "{\n\"original\": \"I love programming\",\n\"translated\": \"Ich liebe Programmieren\"\n}",
"response_metadata": { ... },
"usage_metadata": {
"input_tokens": 47,
"output_tokens": 20,
"total_tokens": 67
}
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ const pinecone = new Pinecone();

const pineconeIndex = pinecone.Index(process.env.PINECONE_INDEX!);

/**
* Pinecone allows you to partition the records in an index into namespaces.
* Queries and other operations are then limited to one namespace,
* so different requests can search different subsets of your index.
* Read more about namespaces here: https://docs.pinecone.io/guides/indexes/use-namespaces
*
* NOTE: If you have namespace enabled in your Pinecone index, you must provide the namespace when creating the PineconeStore.
*/
const namespace = "pinecone";

const pineconeVectorstore = await PineconeStore.fromExistingIndex(
new OpenAIEmbeddings(),
{ pineconeIndex }
{ pineconeIndex, namespace }
);

const pineconeMmrRetriever = pineconeVectorstore.asRetriever({
Expand Down
24 changes: 16 additions & 8 deletions libs/langchain-scripts/bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,15 @@ async function listExternals(
export async function checkTreeShaking(options) {
const externals = await listExternals(options?.extraInternals ?? []);
const entrypoints = await listEntrypoints();
const consoleLog = console.log;
/** @type {Map<string, { log: string; hasSideEffects: boolean; }>} */
const consoleInfo = console.info;
const reportMap = new Map();

for (const entrypoint of entrypoints) {
let sideEffects = "";

console.log = function (...args) {
console.info = function (...args) {
const line = args.length ? args.join(" ") : "";
if (line.trim().startsWith("First side effect in")) {
if (line.includes("First side effect in")) {
sideEffects += `${line}\n`;
}
};
Expand All @@ -245,17 +244,26 @@ export async function checkTreeShaking(options) {
experimentalLogSideEffects: true,
});

let hasUnexpectedSideEffects = sideEffects.length > 0;
if (hasUnexpectedSideEffects) {
const entrypointContent = await fs.promises.readFile(`./dist/${entrypoint.replace(/^\.\//, "")}`);
// Allow escaping side effects strictly within code directly
// within an entrypoint
hasUnexpectedSideEffects = !entrypointContent
.toString()
.includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */");
}
reportMap.set(entrypoint, {
log: sideEffects,
hasSideEffects: sideEffects.length > 0,
hasUnexpectedSideEffects,
});
}

console.log = consoleLog;
console.info = consoleInfo;

let failed = false;
for (const [entrypoint, report] of reportMap) {
if (report.hasSideEffects) {
if (report.hasUnexpectedSideEffects) {
failed = true;
console.log("---------------------------------");
console.log(`Tree shaking failed for ${entrypoint}`);
Expand All @@ -264,7 +272,7 @@ export async function checkTreeShaking(options) {
}

if (failed) {
process.exit(1);
throw new Error("Tree shaking checks failed.");
} else {
console.log("Tree shaking checks passed!");
}
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/scripts",
"version": "0.0.15",
"version": "0.0.18",
"description": "Shared scripts for LangChain.js",
"type": "module",
"engines": {
Expand Down
28 changes: 20 additions & 8 deletions libs/langchain-scripts/src/build_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,16 +441,16 @@ async function checkTreeShaking(config: LangChainConfig) {
);
const externals = listExternals(packageJson, config?.internals ?? []);
const entrypoints = listEntrypoints(packageJson);
const consoleLog = console.log;
/** @type {Map<string, { log: string; hasSideEffects: boolean; }>} */
const consoleInfo = console.info;
/** @type {Map<string, { log: string; hasUnexpectedSideEffects: boolean; }>} */
const reportMap = new Map();

for (const entrypoint of entrypoints) {
let sideEffects = "";

console.log = function (...args) {
console.info = function (...args) {
const line = args.length ? args.join(" ") : "";
if (line.trim().startsWith("First side effect in")) {
if (line.includes("First side effect in")) {
sideEffects += `${line}\n`;
}
};
Expand All @@ -461,17 +461,28 @@ async function checkTreeShaking(config: LangChainConfig) {
experimentalLogSideEffects: true,
});

let hasUnexpectedSideEffects = sideEffects.length > 0;
if (hasUnexpectedSideEffects) {
const entrypointContent = await fs.promises.readFile(
`./dist/${entrypoint.replace(/^\.\//, "")}`
);
// Allow escaping side effects strictly within code directly
// within an entrypoint
hasUnexpectedSideEffects = !entrypointContent
.toString()
.includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */");
}
reportMap.set(entrypoint, {
log: sideEffects,
hasSideEffects: sideEffects.length > 0,
hasUnexpectedSideEffects,
});
}

console.log = consoleLog;
console.info = consoleInfo;

let failed = false;
for (const [entrypoint, report] of reportMap) {
if (report.hasSideEffects) {
if (report.hasUnexpectedSideEffects) {
failed = true;
console.log("---------------------------------");
console.log(`Tree shaking failed for ${entrypoint}`);
Expand All @@ -480,7 +491,8 @@ async function checkTreeShaking(config: LangChainConfig) {
}

if (failed) {
process.exit(1);
// TODO: Throw a hard error here
console.log("Tree shaking checks failed.");
} else {
console.log("Tree shaking checks passed!");
}
Expand Down
27 changes: 19 additions & 8 deletions libs/langchain-scripts/src/check-tree-shaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ async function listExternals(
export async function checkTreeShaking(options?: TreeShakingArgs) {
const externals = await listExternals(options?.extraInternals ?? []);
const entrypoints = await listEntrypoints();
const consoleLog = console.log;
const consoleInfo = console.info;
const reportMap: Map<
string,
{
log: string;
hasSideEffects: boolean;
hasUnexpectedSideEffects: boolean;
}
> = new Map();

for (const entrypoint of entrypoints) {
let sideEffects = "";

console.log = function (...args) {
console.info = function (...args) {
const line = args.length ? args.join(" ") : "";
if (line.trim().startsWith("First side effect in")) {
if (line.includes("First side effect in")) {
sideEffects += `${line}\n`;
}
};
Expand All @@ -85,17 +85,28 @@ export async function checkTreeShaking(options?: TreeShakingArgs) {
experimentalLogSideEffects: true,
});

let hasUnexpectedSideEffects = sideEffects.length > 0;
if (hasUnexpectedSideEffects) {
const entrypointContent = await fs.readFile(
`./dist/${entrypoint.replace(/^\.\//, "")}`
);
// Allow escaping side effects strictly within code directly
// within an entrypoint
hasUnexpectedSideEffects = !entrypointContent
.toString()
.includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */");
}
reportMap.set(entrypoint, {
log: sideEffects,
hasSideEffects: sideEffects.length > 0,
hasUnexpectedSideEffects,
});
}

console.log = consoleLog;
console.info = consoleInfo;

let failed = false;
for (const [entrypoint, report] of reportMap) {
if (report.hasSideEffects) {
if (report.hasUnexpectedSideEffects) {
failed = true;
console.log("---------------------------------");
console.log(`Tree shaking failed for ${entrypoint}`);
Expand All @@ -104,7 +115,7 @@ export async function checkTreeShaking(options?: TreeShakingArgs) {
}

if (failed) {
process.exit(1);
throw new Error("Tree shaking checks failed.");
} else {
console.log("Tree shaking checks passed!");
}
Expand Down

0 comments on commit 41ce3e1

Please sign in to comment.