feat(agent): Agent tweaks

This commit is contained in:
Willie Zutz 2025-06-10 23:34:26 -06:00
parent e707bc630f
commit 29146a03dc

View file

@ -1,6 +1,7 @@
import { Embeddings } from '@langchain/core/embeddings'; import { Embeddings } from '@langchain/core/embeddings';
import { BaseChatModel } from '@langchain/core/language_models/chat_models'; import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { import {
AIMessage,
BaseMessage, BaseMessage,
HumanMessage, HumanMessage,
SystemMessage, SystemMessage,
@ -166,7 +167,7 @@ export class AgentSearch {
return new Command({ return new Command({
goto: 'analyzer', goto: 'analyzer',
update: { update: {
messages: [new SystemMessage('No relevant documents found.')], messages: [new AIMessage('No relevant documents found.')],
}, },
}); });
} }
@ -177,14 +178,14 @@ export class AgentSearch {
return new Command({ return new Command({
goto: 'analyzer', goto: 'analyzer',
update: { update: {
messages: [new SystemMessage(responseMessage)], messages: [new AIMessage(responseMessage)],
relevantDocuments: documents, relevantDocuments: documents,
bannedUrls: bannedUrls, bannedUrls: bannedUrls,
}, },
}); });
} catch (error) { } catch (error) {
console.error('Web search error:', error); console.error('Web search error:', error);
const errorMessage = new SystemMessage( const errorMessage = new AIMessage(
`Web search failed: ${error instanceof Error ? error.message : 'Unknown error'}`, `Web search failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
); );
@ -208,8 +209,7 @@ export class AgentSearch {
- The content should completely address the query, providing detailed explanations, relevant facts, and necessary context - The content should completely address the query, providing detailed explanations, relevant facts, and necessary context
- Use the content provided in the \`context\` tag, as well as the historical context of the conversation, to make your determination - Use the content provided in the \`context\` tag, as well as the historical context of the conversation, to make your determination
- If the context provides conflicting information, explain the discrepancies and what additional information is needed to resolve them - If the context provides conflicting information, explain the discrepancies and what additional information is needed to resolve them
- If the user is asking for a specific number of sources, ensure that we have enough sources to meet that requirement - If the user is asking for a specific number of sources and the context does not provide enough, consider the content insufficient
- Today's date is ${formatDateForLLM(new Date())}
# Output Format # Output Format
- If the content is sufficient, respond with "good_content" in an <answer> XML tag - If the content is sufficient, respond with "good_content" in an <answer> XML tag
@ -244,21 +244,21 @@ ${state.searchInstructionHistory.map((question) => ` - ${question}`).join('\n')
# Context # Context
<context> <context>
Today's date is ${formatDateForLLM(new Date())}
{context} {context}
</context> </context>`;
`;
const analysisPrompt = await ChatPromptTemplate.fromTemplate( const analysisPrompt = await ChatPromptTemplate.fromTemplate(
analysisPromptTemplate, analysisPromptTemplate,
).format({ ).format({
systemInstructions: this.systemInstructions, systemInstructions: this.systemInstructions,
context: state.relevantDocuments context: state.relevantDocuments
.map((doc) => doc.pageContent) .map((doc, index) => `<source${index + 1}>${doc?.metadata?.title ? `<title>${doc?.metadata?.title}</title>` : ''}<content>${doc.pageContent}</content></source${index + 1}>`)
.join('\n\n'), .join('\n\n'),
}); });
const response = await this.llm.invoke( const response = await this.llm.invoke(
[...state.messages, new SystemMessage(analysisPrompt)], [...state.messages, new AIMessage(analysisPrompt)],
{ signal: this.signal }, { signal: this.signal },
); );
@ -286,7 +286,7 @@ ${state.searchInstructionHistory.map((question) => ` - ${question}`).join('\n')
goto: 'web_search', goto: 'web_search',
update: { update: {
messages: [ messages: [
new SystemMessage( new AIMessage(
`The following question can help refine the search: ${moreInfoQuestion}`, `The following question can help refine the search: ${moreInfoQuestion}`,
), ),
], ],
@ -300,7 +300,7 @@ ${state.searchInstructionHistory.map((question) => ` - ${question}`).join('\n')
goto: 'synthesizer', goto: 'synthesizer',
update: { update: {
messages: [ messages: [
new SystemMessage( new AIMessage(
`Analysis completed. We have sufficient information to answer the query.`, `Analysis completed. We have sufficient information to answer the query.`,
), ),
], ],
@ -308,7 +308,7 @@ ${state.searchInstructionHistory.map((question) => ` - ${question}`).join('\n')
}); });
} catch (error) { } catch (error) {
console.error('Analysis error:', error); console.error('Analysis error:', error);
const errorMessage = new SystemMessage( const errorMessage = new AIMessage(
`Analysis failed: ${error instanceof Error ? error.message : 'Unknown error'}`, `Analysis failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
); );
@ -444,7 +444,7 @@ ${doc.metadata?.url.toLowerCase().includes('file') ? '' : '\n<url>' + doc.metada
}); });
} catch (error) { } catch (error) {
console.error('Synthesis error:', error); console.error('Synthesis error:', error);
const errorMessage = new SystemMessage( const errorMessage = new AIMessage(
`Failed to synthesize answer: ${error instanceof Error ? error.message : 'Unknown error'}`, `Failed to synthesize answer: ${error instanceof Error ? error.message : 'Unknown error'}`,
); );