### Start Development Server Source: https://github.com/trendy-design/llmchat/blob/main/README.md Starts the development server using Bun or Yarn. ```bash bun dev # or yarn dev ``` -------------------------------- ### Copy Environment Example Source: https://github.com/trendy-design/llmchat/blob/main/packages/ai/README.md Copies the example environment file to a local configuration file. ```bash cp .env.example .env.local ``` -------------------------------- ### Install Dependencies Source: https://github.com/trendy-design/llmchat/blob/main/packages/ai/README.md Installs the necessary project dependencies using pnpm. ```bash pnpm install ``` -------------------------------- ### Install Dependencies Source: https://github.com/trendy-design/llmchat/blob/main/README.md Installs project dependencies using Bun or Yarn. ```bash bun install # or yarn install ``` -------------------------------- ### Run Customer Support Example Source: https://github.com/trendy-design/llmchat/blob/main/packages/ai/README.md Executes the customer support workflow example using ts-node. ```bash ts-node examples/customer-support-workflow.ts ``` -------------------------------- ### Generate Prisma Client Source: https://github.com/trendy-design/llmchat/blob/main/packages/prisma/README.md Run this command to generate the Prisma client after initial setup or schema changes. ```bash cd packages/prisma bun prisma generate ``` -------------------------------- ### Import and Use Prisma Client in TypeScript Source: https://github.com/trendy-design/llmchat/blob/main/packages/prisma/README.md Import the Prisma client and use it to interact with your database. This example shows creating and retrieving feedback entries. ```typescript import { prisma } from '@repo/prisma'; // Example: Create a new feedback entry async function createFeedback(userId: string, userEmail: string, feedback: string) { return await prisma.feedback.create({ data: { userId, userEmail, feedback, }, }); } // Example: Get all feedback entries async function getAllFeedback() { return await prisma.feedback.findMany(); } ``` -------------------------------- ### Workflow Assembly and Execution Source: https://github.com/trendy-design/llmchat/blob/main/README.md Assembles and executes the defined workflow, starting with an initial query. Exports the workflow as 'researchAgent'. ```typescript builder.addTask(taskPlanner); builder.addTask(informationGatherer); builder.addTask(informationAnalyzer); builder.addTask(reportGenerator); const workflow = builder.build(); workflow.start('taskPlanner', { query: 'Research the impact of AI on healthcare' }); export const researchAgent = workflow; ``` -------------------------------- ### Create Prisma Migration for Production Source: https://github.com/trendy-design/llmchat/blob/main/packages/prisma/README.md Use this command to create and apply database migrations in production environments. ```bash bun prisma migrate dev --name description_of_changes ``` -------------------------------- ### Initialize Core Workflow Components Source: https://github.com/trendy-design/llmchat/blob/main/README.md Set up the event emitter, context, and workflow builder for an agent. This includes initializing the LLM client and ensuring proper typing for events and context. ```typescript import { OpenAI } from 'openai'; import { createTask } from 'task'; import { WorkflowBuilder } from './builder'; import { Context } from './context'; import { TypedEventEmitter } from './events'; // Initialize event emitter with proper typing const events = new TypedEventEmitter(); // Create the workflow builder with proper context const builder = new WorkflowBuilder('research-agent', { events, context: new Context({ query: '', tasks: [], searchResults: [], analysis: '', insights: [], report: '', }), }); // Initialize LLM client const llm = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, }); ``` -------------------------------- ### Handle Customer Inquiry Source: https://github.com/trendy-design/llmchat/blob/main/packages/ai/README.md Demonstrates how to import and use the handleCustomerSupport function to process a customer inquiry and log the result. ```typescript import { handleCustomerSupport } from './examples/customer-support-workflow'; // Handle a customer inquiry const inquiry = "I can't log into my account"; const result = await handleCustomerSupport(inquiry); console.log(result); ``` -------------------------------- ### Clone Repository Source: https://github.com/trendy-design/llmchat/blob/main/README.md Clones the project repository to the local machine. ```bash git clone https://github.com/your-repo/llmchat.git cd llmchat ``` -------------------------------- ### Prisma PostgreSQL Connection String Source: https://github.com/trendy-design/llmchat/blob/main/packages/prisma/README.md Ensure your PostgreSQL connection string is correctly configured in the .env file. ```env DATABASE_URL="postgresql://username:password@localhost:5432/mydb?schema=public" ``` -------------------------------- ### Task Planner: Break down research queries into specific tasks Source: https://github.com/trendy-design/llmchat/blob/main/README.md The taskPlanner breaks down a user's research query into actionable search tasks. It uses an LLM to generate a JSON array of tasks based on the input query. This task is the first step in the research process. ```typescript // Task Planner: Breaks down a research query into specific tasks const taskPlanner = createTask({ name: 'taskPlanner', execute: async ({ context, data }) => { const userQuery = data?.query || 'Research the impact of AI on healthcare'; const planResponse = await llm.chat.completions.create({ model: 'gpt-4o', messages: [ { role: 'system', content: 'You are a task planning assistant that breaks down research queries into specific search tasks.', }, { role: 'user', content: `Break down this research query into specific search tasks: "${userQuery}". Return a JSON array of tasks.`, }, ], response_format: { type: 'json_object' }, }); const content = planResponse.choices[0].message.content || '{"tasks": []}'; const parsedContent = JSON.parse(content); const tasks = parsedContent.tasks || []; context?.set('query', userQuery); context?.set('tasks', tasks); return { tasks, query: userQuery, }; }, route: () => 'informationGatherer', }); ``` -------------------------------- ### Push Schema Changes to Database Source: https://github.com/trendy-design/llmchat/blob/main/packages/prisma/README.md Use this command to apply schema modifications directly to the database during development. ```bash cd packages/prisma bun prisma db push ``` -------------------------------- ### Define Agent Event and Context Types Source: https://github.com/trendy-design/llmchat/blob/main/README.md Define the data structures for events emitted by tasks and the shared context between them. This ensures type safety and clear communication within the agent. ```typescript // Define the events emitted by each task type AgentEvents = { taskPlanner: { tasks: string[]; query: string; }; informationGatherer: { searchResults: string[]; }; informationAnalyzer: { analysis: string; insights: string[]; }; reportGenerator: { report: string; }; }; // Define the shared context between tasks type AgentContext = { query: string; tasks: string[]; searchResults: string[]; analysis: string; insights: string[]; report: string; }; ``` -------------------------------- ### Information Gatherer: Search for information based on tasks Source: https://github.com/trendy-design/llmchat/blob/main/README.md The informationGatherer searches for information relevant to the tasks generated by the taskPlanner. It iterates through each task, queries an LLM for factual data, and collects the search results. This task depends on the 'taskPlanner' to provide the list of tasks. ```typescript // Information Gatherer: Searches for information based on tasks const informationGatherer = createTask({ name: 'informationGatherer', dependencies: ['taskPlanner'], execute: async ({ context, data }) => { const tasks = data.taskPlanner.tasks; const searchResults: string[] = []; // Process each task to gather information for (const task of tasks) { const searchResponse = await llm.chat.completions.create({ model: 'gpt-4o', messages: [ { role: 'system', content: 'You are a search engine that returns factual information.', }, { role: 'user', content: `Search for information about: ${task}. Return relevant facts and data.`, }, ], }); const result = searchResponse.choices[0].message.content || ''; if (result) { searchResults.push(result); } } context?.set('searchResults', searchResults); return { searchResults, }; }, route: () => 'informationAnalyzer', }); ``` -------------------------------- ### Information Analyzer: Analyze gathered information for insights Source: https://github.com/trendy-design/llmchat/blob/main/README.md The informationAnalyzer analyzes the search results collected by the informationGatherer to identify patterns and extract key insights. It uses an LLM to process the aggregated information and provides a structured analysis. This task depends on the 'informationGatherer' to provide the search results. ```typescript // Information Analyzer: Analyzes gathered information for insights const informationAnalyzer = createTask({ name: 'informationAnalyzer', dependencies: ['informationGatherer'], execute: async ({ context, data }) => { const searchResults = data.informationGatherer.searchResults; const query = context?.get('query') || ''; const analysisResponse = await llm.chat.completions.create({ model: 'gpt-4o', messages: [ { role: 'system', content: 'You are an analytical assistant that identifies patterns and extracts insights from information.', }, { role: 'user', content: `Analyze the following information regarding "${query}" and provide a coherent analysis with key insights: ${searchResults.join(' ')}`, }, ], response_format: { type: 'json_object' }, }); const content = analysisResponse.choices[0].message.content || '{"analysis": "", "insights": []}'; const parsedContent = JSON.parse(content); const analysis = parsedContent.analysis || ''; const insights = parsedContent.insights || []; context?.set('analysis', analysis); context?.set('insights', insights); return { analysis, insights, }; }, route: () => 'reportGenerator', }); ``` -------------------------------- ### Report Generator Task Definition Source: https://github.com/trendy-design/llmchat/blob/main/README.md Defines a task for generating a comprehensive report using LLM chat completions. It depends on 'informationAnalyzer' and takes context and data to formulate the report. ```typescript const reportGenerator = createTask({ name: 'reportGenerator', dependencies: ['informationAnalyzer'], execute: async ({ context, data }) => { const { analysis, insights } = data.informationAnalyzer; const { query, searchResults } = context?.getAll() || { query: '', searchResults: [] }; const reportResponse = await llm.chat.completions.create({ model: 'gpt-4o', messages: [ { role: 'system', content: 'You are a report writing assistant that creates comprehensive, well-structured reports.' }, { role: 'user', content: `Create a comprehensive report on "${query}" using the following analysis and insights.\n\nAnalysis: ${analysis}\n\nInsights: ${insights.join('\n- ')}\n\nStructure the report with an executive summary, key findings, detailed analysis, and conclusions.` }, ], }); const report = reportResponse.choices[0].message.content || ''; context?.set('report', report); return { report, }; }, route: () => 'end', }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.