-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
157 lines (140 loc) · 4.59 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import chalk from 'chalk';
import yaml from 'yaml';
import inputPrompt from '@inquirer/input';
import { config } from 'dotenv';
import { ChatOpenAI } from '@langchain/openai';
import { Activity, ActivityKind, Agent } from '@caretakerai/agent';
import { RemoveErrorActivitiesOptimizer, LengthOptimizer } from '@caretakerai/optimizer';
import { fromDocuments, fromExistingIndex } from './store';
config();
const objective = `
You are an Information Retrieval Assistant that helps users find and synthesize information from documents.
**Your responsibilities:**
1. Help users find accurate information through intelligent searching
2. Break down complex questions into multiple focused searches
3. Synthesize information from multiple sources when needed
4. Present information in a clear, readable format
**Search guidelines:**
- Limit searches to 7 attempts per question
- Ensure each search query is unique
- Use the user's language style in queries
- Break complex questions into multiple targeted searches
**Answer formatting:**
- Keep responses under 300 words
- Structure answers in clear paragraphs
- Prefer descriptive prose over bullet points
- Use the user's language style in responses
- Include relevant context and explanations
**Remember to:**
- Start with a friendly introduction
- Explain your search strategy when relevant
- Verify information across multiple searches when needed
- Synthesize information rather than just quoting
- End when the user's question is fully answered
`.trim();
const typeDefs = /* GraphQL */`
schema {
query: Query
mutation: Mutation
}
type Query {
"""
Searches the knowledge base and returns relevant text passages.
Supports single concept searches, multiple parallel searches,
full-text content matching, and semantic similarity matching.
"""
search(input: SearchInput!): SearchResult!
}
type Mutation {
"""
Sends a message to the user's console and captures their typed response.
Displays formatted text messages and returns the complete user input.
"""
say(message: String!): UserResponse!
}
"""
Search parameters for querying the knowledge base
"""
input SearchInput {
"""
Text query that will be matched against the document content.
Supports both exact and semantic matching.
"""
query: String!
}
"""
Container for user interaction responses
"""
type UserResponse {
"""
The complete text entered by the user at the console prompt
"""
reply: String!
}
"""
Container for search operation results
"""
type SearchResult {
"""
Collection of relevant text passages from the knowledge base.
Returns up to 5 most relevant matches.
"""
results: [String]!
}
`.trim();
// Configure LLM model
const llm = new ChatOpenAI({
model: 'gpt-4o-mini',
callbacks: [{ handleLLMStart: (_, [prompt]) => {
console.log(prompt)
} }]
});
async function main() {
// Configure document store
// const store = await fromDocuments(); // Create new index from documents
const store = await fromExistingIndex(); // Alternative: Load existing vector index
// Configure agentic application
const agent = new Agent({
llm, // Language model instance for processing queries
objective, // Define agent's behavior and responsibilities (from objective string above)
maxRetries: 3, // Number of retry attempts for failed operations or LLM completions
typeDefs, // GraphQL schema defining available operations
optimizers: [
new RemoveErrorActivitiesOptimizer(), // Forget interactions that resulted in syntax or execution errors
new LengthOptimizer(32), // Limit interaction history to 16 activities
],
// Initialize conversation greeting the agent
history: [
new Activity({
kind: ActivityKind.Observation,
input: yaml.stringify({
data: {
say: {
reply: 'Hi!, how can you help me?',
},
},
}),
}),
],
// Implementation of GraphQL operations
resolvers: {
Query: {
search: async (_, { input: { query } }) => {
const docs = await store.similaritySearch(query, 3);
const results = docs.map(({ pageContent }) => pageContent)
return { results };
},
},
Mutation: {
say: async (_, { message }) => {
console.log(`${chalk.bold(`AI:`)} ${message}`);
const reply = await inputPrompt({ message: 'Human:' });
return { reply };
},
}
},
});
await agent.invoke();
}
// Start application
main();