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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain[chore]: Remove duplicated code #3487

Merged
merged 6 commits into from
Dec 2, 2023
Merged
Changes from all 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
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { SystemMessage, HumanMessage } from "langchain/schema";

const llamaPath = "/Replace/with/path/to/your/model/gguf-llama2-q4_0.bin";

const model = new ChatLlamaCpp({ modelPath: llamaPath, temperature: 0.7 });
const llamaCpp = new ChatLlamaCpp({ modelPath: llamaPath, temperature: 0.7 });

const stream = await llamaCpp.stream([
new SystemMessage(
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, test, expect, jest } from "@jest/globals";
import { FakeListChatModel } from "../fake.js";
import { HumanMessage } from "../../schema/index.js";
import { StringOutputParser } from "../../schema/output_parser.js";
import { HumanMessage } from "../../../messages/index.js";
import { StringOutputParser } from "../../../output_parsers/string.js";
import { FakeListChatModel } from "../index.js";

describe("Test FakeListChatLLM", () => {
test("Should exist", async () => {
2 changes: 1 addition & 1 deletion langchain/package.json
Original file line number Diff line number Diff line change
@@ -1417,7 +1417,7 @@
},
"dependencies": {
"@anthropic-ai/sdk": "^0.9.1",
"@langchain/core": "~0.0.3",
"@langchain/core": "~0.0.4",
"binary-extensions": "^2.2.0",
"expr-eval": "^2.0.2",
"flat": "^5.0.2",
143 changes: 4 additions & 139 deletions langchain/src/chat_models/fake.ts
Original file line number Diff line number Diff line change
@@ -1,139 +1,4 @@
import { BaseChatModel, BaseChatModelParams } from "./base.js";
import {
AIMessage,
AIMessageChunk,
BaseMessage,
ChatGenerationChunk,
ChatResult,
} from "../schema/index.js";
import { CallbackManagerForLLMRun } from "../callbacks/manager.js";

/**
* Interface for the input parameters specific to the Fake List Chat model.
*/
export interface FakeChatInput extends BaseChatModelParams {
/** Responses to return */
responses: string[];

/** Time to sleep in milliseconds between responses */
sleep?: number;
}

/**
* A fake Chat Model that returns a predefined list of responses. It can be used
* for testing purposes.
* @example
* ```typescript
* const chat = new FakeListChatModel({
* responses: ["I'll callback later.", "You 'console' them!"]
* });
*
* const firstMessage = new HumanMessage("You want to hear a JavaScript joke?");
* const secondMessage = new HumanMessage("How do you cheer up a JavaScript developer?");
*
* // Call the chat model with a message and log the response
* const firstResponse = await chat.call([firstMessage]);
* console.log({ firstResponse });
*
* const secondResponse = await chat.call([secondMessage]);
* console.log({ secondResponse });
* ```
*/
export class FakeListChatModel extends BaseChatModel {
static lc_name() {
return "FakeListChatModel";
}

responses: string[];

i = 0;

sleep?: number;

constructor({ responses, sleep }: FakeChatInput) {
super({});
this.responses = responses;
this.sleep = sleep;
}

_combineLLMOutput() {
return [];
}

_llmType(): string {
return "fake-list";
}

async _generate(
_messages: BaseMessage[],
options?: this["ParsedCallOptions"]
): Promise<ChatResult> {
await this._sleepIfRequested();

if (options?.stop?.length) {
return {
generations: [this._formatGeneration(options.stop[0])],
};
} else {
const response = this._currentResponse();
this._incrementResponse();

return {
generations: [this._formatGeneration(response)],
llmOutput: {},
};
}
}

_formatGeneration(text: string) {
return {
message: new AIMessage(text),
text,
};
}

async *_streamResponseChunks(
_messages: BaseMessage[],
_options: this["ParsedCallOptions"],
_runManager?: CallbackManagerForLLMRun
): AsyncGenerator<ChatGenerationChunk> {
const response = this._currentResponse();
this._incrementResponse();

for await (const text of response) {
await this._sleepIfRequested();
yield this._createResponseChunk(text);
}
}

async _sleepIfRequested() {
if (this.sleep !== undefined) {
await this._sleep();
}
}

async _sleep() {
return new Promise<void>((resolve) => {
setTimeout(() => resolve(), this.sleep);
});
}

_createResponseChunk(text: string): ChatGenerationChunk {
return new ChatGenerationChunk({
message: new AIMessageChunk({ content: text }),
text,
});
}

_currentResponse() {
return this.responses[this.i];
}

_incrementResponse() {
if (this.i < this.responses.length - 1) {
this.i += 1;
} else {
this.i = 0;
}
}
}
export {
type FakeChatInput,
FakeListChatModel,
} from "@langchain/core/utils/testing";
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -7997,7 +7997,7 @@ __metadata:
languageName: unknown
linkType: soft

"@langchain/core@workspace:*, @langchain/core@workspace:langchain-core, @langchain/core@~0.0.3":
"@langchain/core@workspace:*, @langchain/core@workspace:langchain-core, @langchain/core@~0.0.4":
version: 0.0.0-use.local
resolution: "@langchain/core@workspace:langchain-core"
dependencies:
@@ -22607,7 +22607,7 @@ __metadata:
"@gradientai/nodejs-sdk": ^1.2.0
"@huggingface/inference": ^2.6.4
"@jest/globals": ^29.5.0
"@langchain/core": ~0.0.3
"@langchain/core": ~0.0.4
"@mozilla/readability": ^0.4.4
"@notionhq/client": ^2.2.10
"@opensearch-project/opensearch": ^2.2.0