-
Notifications
You must be signed in to change notification settings - Fork 2k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds JSON mode to Ollama and ChatOllama #3229
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
19cdaf0
Update cookbook
jacoblee93 148cb5d
Merge branch 'main' of https://github.com/hwchase17/langchainjs
jacoblee93 1a1aebd
Merge branch 'main' of https://github.com/hwchase17/langchainjs
jacoblee93 25a9206
Adds format option to Ollama
jacoblee93 538dbc9
Fix type
jacoblee93 cf2d957
Format
jacoblee93 2d07c58
Factor out utility type
jacoblee93 f030c57
Update docs
jacoblee93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ChatOllama } from "langchain/chat_models/ollama"; | ||
import { ChatPromptTemplate } from "langchain/prompts"; | ||
|
||
const prompt = ChatPromptTemplate.fromMessages([ | ||
[ | ||
"system", | ||
`You are an expert translator. Format all responses as JSON objects with two keys: "original" and "translated".`, | ||
], | ||
["human", `Translate "{input}" into {language}.`], | ||
]); | ||
|
||
const model = new ChatOllama({ | ||
baseUrl: "http://localhost:11434", // Default value | ||
model: "llama2", // Default value | ||
format: "json", | ||
}); | ||
|
||
const chain = prompt.pipe(model); | ||
|
||
const result = await chain.invoke({ | ||
input: "I love programming", | ||
language: "German", | ||
}); | ||
|
||
console.log(result); | ||
|
||
/* | ||
AIMessage { | ||
content: '{"original": "I love programming", "translated": "Ich liebe das Programmieren"}', | ||
additional_kwargs: {} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { test } from "@jest/globals"; | ||
import { Ollama } from "../ollama.js"; | ||
import { PromptTemplate } from "../../prompts/prompt.js"; | ||
import { BytesOutputParser } from "../../schema/output_parser.js"; | ||
import { | ||
BytesOutputParser, | ||
StringOutputParser, | ||
} from "../../schema/output_parser.js"; | ||
|
||
test.skip("test call", async () => { | ||
const ollama = new Ollama({}); | ||
|
@@ -86,3 +89,25 @@ test.skip("should stream through with a bytes output parser", async () => { | |
console.log(chunks.join("")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is flagging the addition of a new HTTP request using the fetch or axios library in the code, and it would be helpful for maintainers to review this change. |
||
expect(chunks.length).toBeGreaterThan(1); | ||
}); | ||
|
||
test.skip("JSON mode", async () => { | ||
const TEMPLATE = `You are a pirate named Patchy. All responses must be in pirate dialect and in JSON format, with a property named "response" followed by the value. | ||
|
||
User: {input} | ||
AI:`; | ||
|
||
// Infer the input variables from the template | ||
const prompt = PromptTemplate.fromTemplate(TEMPLATE); | ||
|
||
const ollama = new Ollama({ | ||
model: "llama2", | ||
baseUrl: "http://127.0.0.1:11434", | ||
format: "json", | ||
}); | ||
const outputParser = new StringOutputParser(); | ||
const chain = prompt.pipe(ollama).pipe(outputParser); | ||
const res = await chain.invoke({ | ||
input: `Translate "I love programming" into German.`, | ||
}); | ||
expect(JSON.parse(res).response).toBeDefined(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Represents a string value with autocompleted, but not required, suggestions. | ||
*/ | ||
|
||
export type StringWithAutocomplete<T> = T | (string & Record<never, never>); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is flagging the addition of a new HTTP request using the fetch or axios library in the code, and it would be helpful for maintainers to review this change.