generated from langchain-ai/langchain-nextjs-template
-
Notifications
You must be signed in to change notification settings - Fork 313
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
Use AI-Mask within Worker #19
Open
pacoccino
wants to merge
9
commits into
jacoblee93:main
Choose a base branch
from
pacoccino:ai-mask-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
507c4a7
AI Mask within worker
pacoccino 59ed58d
check aiMask client in worker
pacoccino a3f586e
Provider selector & instruct
pacoccino 1dc54ec
use ai mask for embeddings
pacoccino 1794471
fix ChatAIMask constructor
pacoccino 45a827f
Formatting
jacoblee93 35c9b64
cleanup
pacoccino 13abec7
async provideWorkerPort
pacoccino 6718dc3
ollama default
pacoccino 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { | ||
SimpleChatModel, | ||
type BaseChatModelParams, | ||
} from "@langchain/core/language_models/chat_models"; | ||
import type { BaseLanguageModelCallOptions } from "@langchain/core/language_models/base"; | ||
import { BaseMessage, AIMessageChunk } from "@langchain/core/messages"; | ||
import { AIMaskClient, ChatCompletionMessageParam } from "@ai-mask/sdk"; | ||
import { ChatGenerationChunk } from "@langchain/core/outputs"; | ||
|
||
export interface AIMaskInputs extends BaseChatModelParams { | ||
modelId: string; | ||
temperature?: number; | ||
aiMaskClient?: AIMaskClient; | ||
appName?: string; | ||
} | ||
|
||
export interface AIMaskCallOptions extends BaseLanguageModelCallOptions {} | ||
|
||
function convertMessages( | ||
messages: BaseMessage[], | ||
): ChatCompletionMessageParam[] { | ||
return messages.map((message) => { | ||
let role: ChatCompletionMessageParam["role"], | ||
content: ChatCompletionMessageParam["content"]; | ||
if (message._getType() === "human") { | ||
role = "user"; | ||
} else if (message._getType() === "ai") { | ||
role = "assistant"; | ||
} else if (message._getType() === "system") { | ||
role = "system"; | ||
} else { | ||
throw new Error( | ||
`Unsupported message type for AIMask: ${message._getType()}`, | ||
); | ||
} | ||
if (typeof message.content === "string") { | ||
content = message.content; | ||
} else { | ||
throw new Error("unsupported content type"); | ||
} | ||
return { role, content }; | ||
}); | ||
} | ||
|
||
/** | ||
* @example | ||
* ```typescript | ||
* // Initialize the ChatAIMask model with the path to the model binary file. | ||
* const model = new ChatAIMask({ | ||
* modelId: "Mistral-7B-Instruct-v0.2-q4f16_1", | ||
* }); | ||
* | ||
* // Call the model with a message and await the response. | ||
* const response = await model.call([ | ||
* new HumanMessage({ content: "My name is John." }), | ||
* ]); | ||
* | ||
* // Log the response to the console. | ||
* console.log({ response }); | ||
* | ||
* ``` | ||
*/ | ||
export class ChatAIMask extends SimpleChatModel<AIMaskCallOptions> { | ||
static inputs: AIMaskInputs; | ||
|
||
protected _aiMaskClient: AIMaskClient; | ||
|
||
modelId: string; | ||
temperature?: number; | ||
|
||
static lc_name() { | ||
return "ChatAIMask"; | ||
} | ||
|
||
constructor(inputs: AIMaskInputs) { | ||
super(inputs); | ||
|
||
this._aiMaskClient = | ||
inputs?.aiMaskClient ?? new AIMaskClient({ name: inputs?.appName }); | ||
|
||
this.modelId = inputs.modelId; | ||
this.temperature = inputs.temperature; | ||
} | ||
|
||
_llmType() { | ||
return "ai-mask"; | ||
} | ||
|
||
async *_streamResponseChunks( | ||
messages: BaseMessage[], | ||
): AsyncGenerator<ChatGenerationChunk> { | ||
const stream = await this._aiMaskClient.chat( | ||
{ | ||
messages: convertMessages(messages), | ||
temperature: this.temperature, | ||
}, | ||
{ | ||
modelId: this.modelId, | ||
stream: true, | ||
}, | ||
); | ||
|
||
for await (const chunk of stream) { | ||
const text = chunk; | ||
yield new ChatGenerationChunk({ | ||
text, | ||
message: new AIMessageChunk({ | ||
content: text, | ||
}), | ||
}); | ||
} | ||
return stream; | ||
} | ||
|
||
async _call(messages: BaseMessage[]): Promise<string> { | ||
const completion = await this._aiMaskClient.chat( | ||
{ | ||
messages: convertMessages(messages), | ||
temperature: this.temperature, | ||
}, | ||
{ | ||
modelId: this.modelId, | ||
}, | ||
); | ||
return completion; | ||
} | ||
} |
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,108 @@ | ||
import { Pipeline, pipeline } from "@xenova/transformers"; | ||
import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings"; | ||
import { chunkArray } from "@langchain/core/utils/chunk_array"; | ||
import { AIMaskClient } from "@ai-mask/sdk"; | ||
|
||
export interface AIMaskEmbeddingsParams extends EmbeddingsParams { | ||
/** Model name to use */ | ||
modelName: string; | ||
|
||
/** | ||
* Timeout to use when making requests to OpenAI. | ||
*/ | ||
timeout?: number; | ||
|
||
/** | ||
* The maximum number of documents to embed in a single request. | ||
*/ | ||
batchSize?: number; | ||
|
||
/** | ||
* Whether to strip new lines from the input text. This is recommended by | ||
* OpenAI, but may not be suitable for all use cases. | ||
*/ | ||
stripNewLines?: boolean; | ||
aiMaskClient?: AIMaskClient; | ||
appName?: string; | ||
} | ||
|
||
/** | ||
* @example | ||
* ```typescript | ||
* const model = new HuggingFaceTransformersEmbeddings({ | ||
* modelName: "Xenova/all-MiniLM-L6-v2", | ||
* }); | ||
* | ||
* // Embed a single query | ||
* const res = await model.embedQuery( | ||
* "What would be a good company name for a company that makes colorful socks?" | ||
* ); | ||
* console.log({ res }); | ||
* | ||
* // Embed multiple documents | ||
* const documentRes = await model.embedDocuments(["Hello world", "Bye bye"]); | ||
* console.log({ documentRes }); | ||
* ``` | ||
*/ | ||
export class AIMaskEmbeddings | ||
extends Embeddings | ||
implements AIMaskEmbeddingsParams | ||
{ | ||
modelName = "Xenova/all-MiniLM-L6-v2"; | ||
|
||
batchSize = 512; | ||
|
||
stripNewLines = true; | ||
|
||
timeout?: number; | ||
|
||
protected _aiMaskClient: AIMaskClient; | ||
|
||
constructor(fields?: Partial<AIMaskEmbeddingsParams>) { | ||
super(fields ?? {}); | ||
|
||
this.modelName = fields?.modelName ?? this.modelName; | ||
this.stripNewLines = fields?.stripNewLines ?? this.stripNewLines; | ||
this.timeout = fields?.timeout; | ||
|
||
this._aiMaskClient = | ||
fields?.aiMaskClient ?? new AIMaskClient({ name: fields?.appName }); | ||
} | ||
|
||
async embedDocuments(texts: string[]): Promise<number[][]> { | ||
const batches = chunkArray( | ||
this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, | ||
this.batchSize, | ||
); | ||
|
||
const batchRequests = batches.map((batch) => this.runEmbedding(batch)); | ||
const batchResponses = await Promise.all(batchRequests); | ||
const embeddings: number[][] = []; | ||
|
||
for (let i = 0; i < batchResponses.length; i += 1) { | ||
const batchResponse = batchResponses[i]; | ||
for (let j = 0; j < batchResponse.length; j += 1) { | ||
embeddings.push(batchResponse[j]); | ||
} | ||
} | ||
|
||
return embeddings; | ||
} | ||
|
||
async embedQuery(text: string): Promise<number[]> { | ||
const data = await this.runEmbedding([ | ||
this.stripNewLines ? text.replace(/\n/g, " ") : text, | ||
]); | ||
return data[0]; | ||
} | ||
|
||
private async runEmbedding(texts: string[]) { | ||
return this.caller.call(async () => { | ||
const output = await this._aiMaskClient.featureExtraction( | ||
{ texts, pooling: "mean", normalize: true }, | ||
{ modelId: this.modelName }, | ||
); | ||
return output; | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.
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.
See below, could this just trigger on some
initialization
event, then send ainitializationComplete
once thisawait
returns or it times out?