Skip to content
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 8 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions langchain/src/chat_models/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export class ChatOllama

vocabOnly?: boolean;

format?: string;

constructor(fields: OllamaInput & BaseChatModelParams) {
super(fields);
this.model = fields.model ?? this.model;
Expand Down Expand Up @@ -130,6 +132,7 @@ export class ChatOllama
this.useMLock = fields.useMLock;
this.useMMap = fields.useMMap;
this.vocabOnly = fields.vocabOnly;
this.format = fields.format;
}

_llmType() {
Expand All @@ -145,6 +148,7 @@ export class ChatOllama
invocationParams(options?: this["ParsedCallOptions"]) {
return {
model: this.model,
format: this.format,
options: {
embedding_only: this.embeddingOnly,
f16_kv: this.f16KV,
Expand Down
27 changes: 26 additions & 1 deletion langchain/src/chat_models/tests/chatollama.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { AIMessage, HumanMessage } from "../../schema/index.js";
import { LLMChain } from "../../chains/llm_chain.js";
import { PromptTemplate } from "../../prompts/prompt.js";
import { BufferMemory } from "../../memory/buffer_memory.js";
import { BytesOutputParser } from "../../schema/output_parser.js";
import {
BytesOutputParser,
StringOutputParser,
} from "../../schema/output_parser.js";

test.skip("test call", async () => {
const ollama = new ChatOllama({});
Expand Down Expand Up @@ -129,3 +132,25 @@ test.skip("should stream through with a bytes output parser", async () => {
console.log(chunks.join(""));
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 ChatOllama({
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();
});
4 changes: 4 additions & 0 deletions langchain/src/llms/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export class Ollama extends LLM<OllamaCallOptions> implements OllamaInput {

vocabOnly?: boolean;

format?: string;

constructor(fields: OllamaInput & BaseLLMParams) {
super(fields);
this.model = fields.model ?? this.model;
Expand Down Expand Up @@ -119,6 +121,7 @@ export class Ollama extends LLM<OllamaCallOptions> implements OllamaInput {
this.useMLock = fields.useMLock;
this.useMMap = fields.useMMap;
this.vocabOnly = fields.vocabOnly;
this.format = fields.format;
}

_llmType() {
Expand All @@ -128,6 +131,7 @@ export class Ollama extends LLM<OllamaCallOptions> implements OllamaInput {
invocationParams(options?: this["ParsedCallOptions"]) {
return {
model: this.model,
format: this.format,
options: {
embedding_only: this.embeddingOnly,
f16_kv: this.f16KV,
Expand Down
27 changes: 26 additions & 1 deletion langchain/src/llms/tests/ollama.int.test.ts
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({});
Expand Down Expand Up @@ -86,3 +89,25 @@ test.skip("should stream through with a bytes output parser", async () => {
console.log(chunks.join(""));
Copy link

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.

Copy link

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.

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();
});
2 changes: 2 additions & 0 deletions langchain/src/util/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export interface OllamaInput {
useMLock?: boolean;
useMMap?: boolean;
vocabOnly?: boolean;
format?: string;
}

export interface OllamaRequestParams {
model: string;
prompt: string;
format?: string;
options: {
embedding_only?: boolean;
f16_kv?: boolean;
Expand Down
Loading