Skip to content

Commit

Permalink
types: move types to separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed May 24, 2024
1 parent 52bef47 commit 472d4de
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 128 deletions.
30 changes: 30 additions & 0 deletions src/dtos/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* The context input for the agent.
*/
export interface ContextInput {
input: string
chat_history: string
episodic_memory: string
declarative_memory: string
[key: string]: string
}

/**
* The intermediate step of the agent.
*/
export interface IntermediateStep {
tool: string
input: string | null
observation: string
}

/**
* The agent reply configuration.
*/
export interface AgentFastReply {
output: string
returnDirect?: boolean
intermediateSteps?: IntermediateStep[]
}

export type InstantToolTrigger = `${string}{name}${string}`
77 changes: 77 additions & 0 deletions src/dtos/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { DocumentInput } from '@langchain/core/documents'
import type { EmbeddedVector, FilterMatch } from './vector-memory.ts'

/**
* The configuration for memory recall.
*/
export interface MemoryRecallConfig {
embedding: number[]
k: number
threshold: number
filter?: Record<string, FilterMatch>
}

/**
* The configurations for each memory recall.
*/
export interface MemoryRecallConfigs {
episodic: MemoryRecallConfig
declarative: MemoryRecallConfig
procedural: MemoryRecallConfig
[key: string]: MemoryRecallConfig
}

/**
* A memory document.
*/
export type MemoryDocument = {
id: string
vector: EmbeddedVector
score: number
} & DocumentInput

/**
* The working memory of the cat.
*/
export interface WorkingMemory {
episodic: MemoryDocument[]
declarative: MemoryDocument[]
procedural: MemoryDocument[]
[key: string]: MemoryDocument[]
}

/**
* The content of a memory message.
*/
export interface MemoryMessage {
what: string
who: string
when: number
why?: {
input: string
intermediateSteps: Record<string, any>[]
memory?: WorkingMemory
}
}

/**
* A message object sent by the user.
*/
export interface Message {
text: string
[key: string]: any
}

/**
* A message object sent by the websocket.
*/
export type WSMessage = {
type: 'error'
name: string
description: string
} | {
type: 'token' | 'notification'
content: string
} | ({
type: 'chat'
} & MemoryMessage)
43 changes: 43 additions & 0 deletions src/dtos/vector-memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { VectorMemoryCollection } from '@memory/vector-memory-collection.ts'
import type { Schemas } from '@qdrant/js-client-rest'

export type Filter = Schemas['Filter']

export type FilterCondition = Schemas['FieldCondition']

export type FilterMatch = FilterCondition['match']

export type PointData = Schemas['PointStruct']

export type EmbeddedVector = Schemas['NamedVectorStruct']

/**
* The configuration of a vector memory.
*/
export interface VectorMemoryConfig {
embedderName: string
embedderSize: number
}

/**
* The configurations for each vector memory.
*/
export interface VectorMemoryCollections {
episodic: VectorMemoryCollection
declarative: VectorMemoryCollection
procedural: VectorMemoryCollection
[key: string]: VectorMemoryCollection
}

/**
* The accepted JSON format for an imported memory.
*/
export interface MemoryJson {
embedder: string
collections: {
declarative: PointData[]
procedural: PointData[]
episodic: PointData[]
[key: string]: PointData[]
}
}
32 changes: 6 additions & 26 deletions src/looking_glass/agent-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,20 @@ import { type Form, FormState, type Tool, isTool, madHatter } from '@mh'
import { parsedEnv } from '@utils'
import { log } from '@logger'
import { db } from '@db'
import type { MemoryDocument, MemoryMessage } from '@dto/message.ts'
import type { AgentFastReply, ContextInput, IntermediateStep } from '@dto/agent.ts'
import { MAIN_PROMPT_PREFIX, MAIN_PROMPT_SUFFIX, TOOL_PROMPT, ToolPromptTemplate } from './prompts.ts'
import type { MemoryDocument, MemoryMessage, StrayCat } from './stray-cat.ts'
import type { StrayCat } from './stray-cat.ts'
import { ProceduresOutputParser } from './output-parser.ts'
import { NewTokenHandler } from './callbacks.ts'

export interface AgentInput {
input: string
chat_history: string
episodic_memory: string
declarative_memory: string
[key: string]: string
}

export interface IntermediateStep {
tool: string
input: string | null
observation: string
}

export interface AgentFastReply {
output: string
returnDirect?: boolean
intermediateSteps?: IntermediateStep[]
}

export type InstantToolTrigger = `${string}{name}${string}`

/**
* Manager of Langchain Agent.
* This class manages the Agent that uses the LLM. It takes care of formatting the prompt and filtering the tools
* before feeding them to the Agent. It also instantiates the Langchain Agent.
*/
export class AgentManager {
async executeProceduresChain(agentInput: AgentInput, stray: StrayCat) {
async executeProceduresChain(agentInput: ContextInput, stray: StrayCat) {
const recalledProcedures = stray.workingMemory.procedural.filter((p) => {
return ['tool', 'form'].includes(p.metadata?.type)
&& ['description', 'startExample'].includes(p.metadata?.trigger)
Expand Down Expand Up @@ -126,7 +106,7 @@ export class AgentManager {
}
}

async executeMemoryChain(input: AgentInput, stray: StrayCat) {
async executeMemoryChain(input: ContextInput, stray: StrayCat) {
const prefix = madHatter.executeHook('agentPromptPrefix', MAIN_PROMPT_PREFIX, stray)
const suffix = madHatter.executeHook('agentPromptSuffix', MAIN_PROMPT_SUFFIX, stray)

Expand All @@ -147,7 +127,7 @@ export class AgentManager {
}, { callbacks: [new NewTokenHandler(stray)] })
}

async executeTool(input: AgentInput, stray: StrayCat): Promise<AgentFastReply | undefined> {
async executeTool(input: ContextInput, stray: StrayCat): Promise<AgentFastReply | undefined> {
const trigger = madHatter.executeHook('instantToolTrigger', '@{name}', stray)

if (!trigger) return undefined
Expand Down
3 changes: 2 additions & 1 deletion src/looking_glass/cheshire-cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import type { Embeddings } from '@langchain/core/embeddings'
import type { BaseLanguageModel } from '@langchain/core/language_models/base'
import { getEmbedder, getLLM } from '@factory'
import { type Form, type Tool, isForm, isTool, madHatter } from '@mh'
import { type PointData, type VectorMemory, getVectorMemory } from '@memory'
import { type VectorMemory, getVectorMemory } from '@memory'
import { db } from '@db'
import { log } from '@logger'
import type { PointData } from '@dto/vector-memory.ts'
import { AgentManager } from './agent-manager.ts'
import { StrayCat } from './stray-cat.ts'

Expand Down
53 changes: 2 additions & 51 deletions src/looking_glass/stray-cat.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,17 @@
import type { RawData, WebSocket } from 'ws'
import callsites from 'callsites'
import type { BaseCallbackHandler } from '@langchain/core/callbacks/base'
import type { DocumentInput } from '@langchain/core/documents'
import { Document } from '@langchain/core/documents'
import { destr } from 'destr'
import { type PluginManifest, madHatter } from '@mh'
import type { EmbeddedVector, FilterMatch } from '@memory'
import type { Message } from '@utils'
import { log } from '@logger'
import { rabbitHole } from '@rh'
import type { ChainValues } from '@langchain/core/utils/types'
import type { AgentFastReply } from './agent-manager.ts'
import type { MemoryMessage, MemoryRecallConfigs, Message, WSMessage, WorkingMemory } from '@dto/message.ts'
import type { AgentFastReply } from '@dto/agent.ts'
import { NewTokenHandler } from './callbacks.ts'
import { cheshireCat } from './cheshire-cat.ts'

export interface MemoryRecallConfig {
embedding: number[]
k: number
threshold: number
filter?: Record<string, FilterMatch>
}

export interface MemoryRecallConfigs {
episodic: MemoryRecallConfig
declarative: MemoryRecallConfig
procedural: MemoryRecallConfig
[key: string]: MemoryRecallConfig
}

export type MemoryDocument = {
id: string
vector: EmbeddedVector
score: number
} & DocumentInput

export interface WorkingMemory {
episodic: MemoryDocument[]
declarative: MemoryDocument[]
procedural: MemoryDocument[]
[key: string]: MemoryDocument[]
}

export interface MemoryMessage {
what: string
who: string
when: number
why?: {
input: string
intermediateSteps: Record<string, any>[]
memory?: WorkingMemory
}
}

export type WSMessage = {
type: 'error'
name: string
description: string
} | {
type: 'token' | 'notification'
content: string
} | ({ type: 'chat' } & MemoryMessage)

export class StrayCat {
private chatHistory: MemoryMessage[] = []
private _ws?: WebSocket
Expand Down
3 changes: 2 additions & 1 deletion src/mad_hatter/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import _Merge from 'lodash/merge.js'
import { LLMChain } from 'langchain/chains'
import { PromptTemplate } from '@langchain/core/prompts'
import { kebabCase } from 'scule'
import type { AgentFastReply, StrayCat } from '@lg'
import type { StrayCat } from '@lg'
import { log } from '@logger'
import { parseJson, parsedEnv } from '@utils'
import type { AgentFastReply } from '@dto/agent.ts'

export enum FormState {
/**
Expand Down
7 changes: 4 additions & 3 deletions src/mad_hatter/hook.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Document } from '@langchain/core/documents'
import type { TextSplitter } from 'langchain/text_splitter'
import type { ChainValues } from '@langchain/core/utils/types'
import type { AgentFastReply, AgentInput, CheshireCat, InstantToolTrigger, MemoryMessage, MemoryRecallConfigs, StrayCat } from '@lg'
import type { CheshireCat, StrayCat } from '@lg'
import type { EmbedderSettings, LLMSettings } from '@factory'
import type { VectorMemoryCollection } from '@memory'
import type { Message } from '@utils'
import type { FileParsers, WebParser } from '@rh'
import type { AgentFastReply, ContextInput, InstantToolTrigger } from '@dto/agent.ts'
import type { MemoryMessage, MemoryRecallConfigs, Message } from '@/dtos/message.ts'

export interface HookTypes {
// Cheshire Cat hooks
Expand All @@ -17,7 +18,7 @@ export interface HookTypes {
// Agent Manager hooks
agentPromptInstructions: (prompt: string, stray: StrayCat) => string
allowedTools: (tools: string[], stray: StrayCat) => string[]
beforeAgentStarts: (input: AgentInput, stray: StrayCat) => AgentInput
beforeAgentStarts: (input: ContextInput, stray: StrayCat) => ContextInput
agentFastReply: (reply: Nullable<AgentFastReply>, stray: StrayCat) => Nullable<AgentFastReply>
agentPromptPrefix: (prefix: string, stray: StrayCat) => string
agentPromptSuffix: (suffix: string, stray: StrayCat) => string
Expand Down
14 changes: 2 additions & 12 deletions src/memory/vector-memory-collection.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import { join } from 'node:path'
import { lstatSync, mkdirSync, renameSync, writeFileSync } from 'node:fs'
import type { Schemas } from '@qdrant/js-client-rest'
import { randomUUID } from 'uncrypto'
import { ofetch } from 'ofetch'
import type { MemoryDocument } from '@lg'
import { parsedEnv } from '@utils'
import { log } from '@logger'
import type { EmbeddedVector, Filter, FilterCondition, FilterMatch, PointData } from '@dto/vector-memory.ts'
import type { MemoryDocument } from '@dto/message.ts'
import { vectorDb } from './vector-memory.ts'

export type Filter = Schemas['Filter']

export type FilterCondition = Schemas['FieldCondition']

export type FilterMatch = FilterCondition['match']

export type PointData = Schemas['PointStruct']

export type EmbeddedVector = Schemas['NamedVectorStruct']

export class VectorMemoryCollection {
private constructor(public name: string, public embedderName: string, public embedderSize: number) {}

Expand Down
13 changes: 1 addition & 12 deletions src/memory/vector-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@ import { QdrantClient } from '@qdrant/js-client-rest'
import { parsedEnv } from '@utils'
import { madHatter } from '@mh'
import { log } from '@logger'
import type { VectorMemoryCollections, VectorMemoryConfig } from '@dto/vector-memory.ts'
import { VectorMemoryCollection } from './vector-memory-collection.ts'

export interface VectorMemoryConfig {
embedderName: string
embedderSize: number
}

export interface VectorMemoryCollections {
episodic: VectorMemoryCollection
declarative: VectorMemoryCollection
procedural: VectorMemoryCollection
[key: string]: VectorMemoryCollection
}

const { qdrantApiKey, qdrantHost, qdrantPort, secure } = parsedEnv

export const vectorDb = new QdrantClient({
Expand Down
Loading

0 comments on commit 472d4de

Please sign in to comment.