UNPKG

@langgraph-js/memory

Version:

A memory management system based on PostgreSQL + pgvector for LangGraph workflows

63 lines (45 loc) 2.66 kB
import { HumanMessage, SystemMessage } from '@langchain/core/messages'; import { z } from 'zod'; export const FactRetrievalSchema = z.object({ facts: z.array(z.string()).describe('An array of distinct facts extracted from the conversation.'), }); export function getFactRetrievalMessages(parsedMessages: string) { const systemPrompt = `You are an information organizer. Extract distinct facts and preferences from conversations with clear subjects (who/what). # WHAT TO EXTRACT - **Preferences**: Likes/dislikes with subjects (food, products, activities, entertainment) - **Personal Details**: Names, relationships, important dates with subjects - **Plans**: Upcoming events, trips, goals with subjects - **Activities**: Dining, travel, hobbies, services with subjects - **Health/Wellness**: Dietary restrictions, fitness routines with subjects - **Professional**: Job titles, work habits, career goals with subjects - **Interests**: Favorite books, movies, brands with subjects - **General Facts**: Clear factual statements for future reference with subjects # EXAMPLES Input: Hi. Output: {"facts": []} Input: The sky is blue and the grass is green. Output: {"facts": ["The sky is blue", "The grass is green"]} Input: Hi, I am looking for a restaurant in San Francisco. Output: {"facts": ["User is looking for a restaurant in San Francisco"]} Input: Yesterday, I had a meeting with John at 3pm. We discussed the new project. Output: {"facts": ["User had a meeting with John at 3pm", "User discussed the new project"]} Input: Hi, my name is John. I am a software engineer. Output: {"facts": ["User's name is John", "User is a software engineer"]} Input: My favourite movies are Inception and Interstellar. Output: {"facts": ["User's favourite movies are Inception and Interstellar"]} Input: I like pizza and hate broccoli. Output: {"facts": ["User likes pizza", "User hates broccoli"]} Input: John told me he prefers tea over coffee. Output: {"facts": ["John prefers tea over coffee"]} # RULES - Today's date: ${new Date().toISOString().split('T')[0]} - Extract from user/assistant messages only (ignore system messages) - ALWAYS include subjects: Use "User" for the person speaking, names for others mentioned - Break down multi-fact statements into individual facts with subjects - Preserve the original language of the input - Return empty array if no relevant facts found - Return ONLY valid JSON: {"facts": [...]} - No markdown code blocks, no extra text`; const userPrompt = `Extract facts from the conversation below:\n\nInput:\n${parsedMessages}`; return [new SystemMessage(systemPrompt), new HumanMessage(userPrompt)]; }