Feat/update ollama for function calling (#2892)

update ollama for function calling
This commit is contained in:
Henry Heng 2024-07-27 01:29:36 +01:00 committed by GitHub
parent 1338501658
commit 3e54d53692
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 462 additions and 417 deletions

View File

@ -56,7 +56,10 @@
"onlyBuiltDependencies": [
"faiss-node",
"sqlite3"
]
],
"overrides": {
"@langchain/core": "0.2.18"
}
},
"engines": {
"node": ">=18.15.0 <19.0.0 || ^20",

View File

@ -1,9 +1,8 @@
import { ChatOllama } from '@langchain/community/chat_models/ollama'
import { ChatOllama, ChatOllamaInput } from '@langchain/ollama'
import { BaseChatModelParams } from '@langchain/core/language_models/chat_models'
import { BaseCache } from '@langchain/core/caches'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { OllamaInput } from '@langchain/community/llms/ollama'
import { BaseChatModelParams } from '@langchain/core/language_models/chat_models'
class ChatOllama_ChatModels implements INode {
label: string
@ -20,7 +19,7 @@ class ChatOllama_ChatModels implements INode {
constructor() {
this.label = 'ChatOllama'
this.name = 'chatOllama'
this.version = 2.0
this.version = 3.0
this.type = 'ChatOllama'
this.icon = 'Ollama.svg'
this.category = 'Chat Models'
@ -55,6 +54,15 @@ class ChatOllama_ChatModels implements INode {
default: 0.9,
optional: true
},
{
label: 'Keep Alive',
name: 'keepAlive',
type: 'string',
description: 'How long to keep connection alive. A duration string (such as "10m" or "24h")',
default: '5m',
optional: true,
additionalParams: true
},
{
label: 'Top P',
name: 'topP',
@ -115,16 +123,6 @@ class ChatOllama_ChatModels implements INode {
optional: true,
additionalParams: true
},
{
label: 'Number of GQA groups',
name: 'numGqa',
type: 'number',
description:
'The number of GQA groups in the transformer layer. Required for some models, for example it is 8 for llama2:70b. Refer to <a target="_blank" href="https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values">docs</a> for more details',
step: 1,
optional: true,
additionalParams: true
},
{
label: 'Number of GPU',
name: 'numGpu',
@ -199,17 +197,16 @@ class ChatOllama_ChatModels implements INode {
const mirostatEta = nodeData.inputs?.mirostatEta as string
const mirostatTau = nodeData.inputs?.mirostatTau as string
const numCtx = nodeData.inputs?.numCtx as string
const numGqa = nodeData.inputs?.numGqa as string
const keepAlive = nodeData.inputs?.keepAlive as string
const numGpu = nodeData.inputs?.numGpu as string
const numThread = nodeData.inputs?.numThread as string
const repeatLastN = nodeData.inputs?.repeatLastN as string
const repeatPenalty = nodeData.inputs?.repeatPenalty as string
const stop = nodeData.inputs?.stop as string
const tfsZ = nodeData.inputs?.tfsZ as string
const cache = nodeData.inputs?.cache as BaseCache
const obj: OllamaInput & BaseChatModelParams = {
const obj: ChatOllamaInput & BaseChatModelParams = {
baseUrl,
temperature: parseFloat(temperature),
model: modelName
@ -221,16 +218,12 @@ class ChatOllama_ChatModels implements INode {
if (mirostatEta) obj.mirostatEta = parseFloat(mirostatEta)
if (mirostatTau) obj.mirostatTau = parseFloat(mirostatTau)
if (numCtx) obj.numCtx = parseFloat(numCtx)
if (numGqa) obj.numGqa = parseFloat(numGqa)
if (numGpu) obj.numGpu = parseFloat(numGpu)
if (numThread) obj.numThread = parseFloat(numThread)
if (repeatLastN) obj.repeatLastN = parseFloat(repeatLastN)
if (repeatPenalty) obj.repeatPenalty = parseFloat(repeatPenalty)
if (tfsZ) obj.tfsZ = parseFloat(tfsZ)
if (stop) {
const stopSequences = stop.split(',')
obj.stop = stopSequences
}
if (keepAlive) obj.keepAlive = keepAlive
if (cache) obj.cache = cache
const model = new ChatOllama(obj)

View File

@ -43,6 +43,7 @@ class ChatOllamaFunction_ChatModels implements INode {
this.type = 'ChatOllamaFunction'
this.icon = 'Ollama.svg'
this.category = 'Chat Models'
this.badge = 'DEPRECATING'
this.description = 'Run open-source function-calling compatible LLM on Ollama'
this.baseClasses = [this.type, ...getBaseClasses(OllamaFunctions)]
this.inputs = [

View File

@ -111,6 +111,7 @@ const initializeMongoDB = async (nodeData: INodeData, options: ICommonObject): P
sessionId
})
// @ts-ignore
mongoDBChatMessageHistory.getMessages = async (): Promise<BaseMessage[]> => {
const document = await collection.findOne({
sessionId: (mongoDBChatMessageHistory as any).sessionId
@ -119,6 +120,7 @@ const initializeMongoDB = async (nodeData: INodeData, options: ICommonObject): P
return messages.map(mapStoredMessageToChatMessage)
}
// @ts-ignore
mongoDBChatMessageHistory.addMessage = async (message: BaseMessage): Promise<void> => {
const messages = [message].map((msg) => msg.toDict())
await collection.updateOne(
@ -136,6 +138,7 @@ const initializeMongoDB = async (nodeData: INodeData, options: ICommonObject): P
return new BufferMemoryExtended({
memoryKey: memoryKey ?? 'chat_history',
// @ts-ignore
chatHistory: mongoDBChatMessageHistory,
sessionId,
collection

View File

@ -71,7 +71,8 @@ class StructuredOutputParser implements INode {
const autoFix = nodeData.inputs?.autofixParser as boolean
try {
const structuredOutputParser = LangchainStructuredOutputParser.fromZodSchema(z.object(convertSchemaToZod(jsonStructure)))
const zodSchema = z.object(convertSchemaToZod(jsonStructure)) as any
const structuredOutputParser = LangchainStructuredOutputParser.fromZodSchema(zodSchema)
// NOTE: When we change Flowise to return a json response, the following has to be changed to: JsonStructuredOutputParser
Object.defineProperty(structuredOutputParser, 'autoFix', {

View File

@ -174,7 +174,7 @@ class ChatflowTool extends StructuredTool {
schema = z.object({
input: z.string().describe('input question')
})
}) as any
constructor({
name,

View File

@ -42,6 +42,7 @@ export class DynamicStructuredTool<
func: DynamicStructuredToolInput['func']
// @ts-ignore
schema: T
private variables: any[]
private flowObj: any

View File

@ -63,7 +63,7 @@ export class ReadFileTool extends StructuredTool {
schema = z.object({
file_path: z.string().describe('name of file')
})
}) as any
name = 'read_file'

View File

@ -77,7 +77,7 @@ class Retriever_Tools implements INode {
const schema = z.object({
input: z.string().describe('input to look up in retriever')
})
}) as any
const tool = new DynamicStructuredTool({ ...input, func, schema })
return tool

View File

@ -64,7 +64,7 @@ export class WriteFileTool extends StructuredTool {
schema = z.object({
file_path: z.string().describe('name of file'),
text: z.string().describe('text to write to file')
})
}) as any
name = 'write_file'

View File

@ -37,7 +37,6 @@
"@langchain/anthropic": "^0.2.1",
"@langchain/cohere": "^0.0.7",
"@langchain/community": "^0.2.17",
"@langchain/core": "^0.2.14",
"@langchain/exa": "^0.0.5",
"@langchain/google-genai": "^0.0.22",
"@langchain/google-vertexai": "^0.0.19",
@ -45,6 +44,7 @@
"@langchain/langgraph": "^0.0.22",
"@langchain/mistralai": "^0.0.26",
"@langchain/mongodb": "^0.0.1",
"@langchain/ollama": "^0.0.2",
"@langchain/openai": "^0.0.30",
"@langchain/pinecone": "^0.0.3",
"@langchain/qdrant": "^0.0.5",
@ -82,7 +82,7 @@
"ioredis": "^5.3.2",
"jsdom": "^22.1.0",
"jsonpointer": "^5.0.1",
"langchain": "^0.2.8",
"langchain": "^0.2.11",
"langfuse": "3.3.4",
"langfuse-langchain": "^3.3.4",
"langsmith": "0.1.6",

File diff suppressed because one or more lines are too long