Skip to content

Commit

Permalink
Merge pull request #112 from SciPhi-AI/Nolan/ConversationHistory
Browse files Browse the repository at this point in the history
Introduce Conversation History
  • Loading branch information
NolanTrem authored Oct 24, 2024
2 parents e91da79 + cf93f44 commit 3f4b101
Show file tree
Hide file tree
Showing 21 changed files with 909 additions and 504 deletions.
13 changes: 7 additions & 6 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ module.exports = {
],
},
env: {
CLOUD_INKEEP_API_KEY: process.env.CLOUD_INKEEP_API_KEY,
CLOUD_INKEEP_INT_ID: process.env.CLOUD_INKEEP_INT_ID,
CLOUD_INKEEP_ORG_ID: process.env.CLOUD_INKEEP_ORG_ID,
CLOUD_DOCS_INKEEP_API_KEY: process.env.CLOUD_DOCS_INKEEP_API_KEY,
CLOUD_DOCS_INKEEP_INT_ID: process.env.CLOUD_DOCS_INKEEP_INT_ID,
CLOUD_DOCS_INKEEP_ORG_ID: process.env.CLOUD_DOCS_INKEEP_ORG_ID,
NEXT_PUBLIC_R2R_DEPLOYMENT_URL: process.env.NEXT_PUBLIC_R2R_DEPLOYMENT_URL,
R2R_DASHBOARD_DISABLE_TELEMETRY:
process.env.R2R_DASHBOARD_DISABLE_TELEMETRY,
SUPABASE_URL: process.env.SUPABASE_URL,
SUPABASE_ANON_KEY: process.env.SUPABASE_ANON_KEY,
NEXT_PUBLIC_HATCHET_DASHBOARD_URL:
process.env.NEXT_PUBLIC_HATCHET_DASHBOARD_URL,
},
};
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
"name": "r2r-dashboard",
"version": "1.0.0",
"private": true,
"proxy": "http://localhost:7272",
"scripts": {
"dev": "NEXT_PUBLIC_CLOUD_MODE=false next dev -p 3005",
"dev:cloud": "NEXT_PUBLIC_CLOUD_MODE=true next dev",
"dev": "next dev -p 3005",
"build": "next build",
"start": "next start",
"lint": "eslint --fix .",
Expand Down Expand Up @@ -92,7 +90,7 @@
"pnpm": "^9.5.0",
"postcss": "^8.4.39",
"posthog-js": "^1.148.0",
"r2r-js": "^0.3.5",
"r2r-js": "^0.3.10",
"radix-ui": "^1.0.1",
"react": "18.3.1",
"react-chartjs-2": "^5.2.0",
Expand Down
477 changes: 251 additions & 226 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions public/images/hatchet-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/components/ChatDemo/ServerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const R2RServerCard: React.FC<R2RServerCardProps> = ({
<PipelineStatus onStatusChange={onStatusChange} />
</div>
<CardDescription className="text-sm">
Your deployment of an R2R server.
Your R2R server deployment.
</CardDescription>
</CardHeader>
<CardContent className="pt-0">
Expand Down
2 changes: 1 addition & 1 deletion src/components/ChatDemo/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ function Table<T extends { [key: string]: any }>({
</table>
</div>
{showPagination && (
<div className="mt-4">
<div>
<Pagination
currentPage={currentPage}
totalPages={totalPages}
Expand Down
111 changes: 88 additions & 23 deletions src/components/ChatDemo/result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export const Result: FC<{
pipelineUrl: string | null;
search_limit: number;
search_filters: Record<string, unknown>;
kg_search_type: 'local' | 'global';
max_llm_queries_for_global_search: number;
rag_temperature: number | null;
rag_topP: number | null;
rag_topK: number | null;
Expand All @@ -43,15 +41,19 @@ export const Result: FC<{
mode: 'rag' | 'rag_agent';
selectedCollectionIds: string[];
onAbortRequest?: () => void;
messages: Message[];
setMessages: React.Dispatch<React.SetStateAction<Message[]>>;
selectedConversationId: string | null;
setSelectedConversationId: React.Dispatch<
React.SetStateAction<string | null>
>;
}> = ({
query,
setQuery,
userId,
pipelineUrl,
search_limit,
search_filters,
kg_search_type,
max_llm_queries_for_global_search,
rag_temperature,
rag_topP,
rag_topK,
Expand All @@ -64,8 +66,11 @@ export const Result: FC<{
mode,
selectedCollectionIds,
onAbortRequest,
messages,
setMessages,
selectedConversationId,
setSelectedConversationId,
}) => {
const [messages, setMessages] = useState<Message[]>([]);
const abortControllerRef = useRef<AbortController | null>(null);
const [isStreaming, setIsStreaming] = useState<boolean>(false);
const [isSearching, setIsSearching] = useState<boolean>(false);
Expand Down Expand Up @@ -100,16 +105,10 @@ export const Result: FC<{
searchPerformed?: boolean
) => {
setMessages((prevMessages) => {
if (prevMessages.length === 0) {
console.warn('Attempted to update last message when no messages exist');
return prevMessages;
}

const updatedMessages = [...prevMessages];
const lastMessage = updatedMessages[updatedMessages.length - 1];
const lastMessage = prevMessages[prevMessages.length - 1];
if (lastMessage.role === 'assistant') {
return [
...updatedMessages.slice(0, -1),
...prevMessages.slice(0, -1),
{
...lastMessage,
...(content !== undefined && { content }),
Expand All @@ -118,9 +117,20 @@ export const Result: FC<{
...(searchPerformed !== undefined && { searchPerformed }),
},
];
} else {
return [
...prevMessages,
{
role: 'assistant',
content: content || '',
id: Date.now().toString(),
timestamp: Date.now(),
isStreaming: isStreaming || false,
sources: sources || {},
searchPerformed: searchPerformed || false,
},
];
}

return prevMessages;
});
};

Expand Down Expand Up @@ -169,11 +179,7 @@ export const Result: FC<{
searchPerformed: false,
};

setMessages((prevMessages) => [
...prevMessages,
newUserMessage,
newAssistantMessage,
]);
setMessages((prevMessages) => [...prevMessages, newUserMessage]);

let buffer = '';
let inLLMResponse = false;
Expand All @@ -188,6 +194,39 @@ export const Result: FC<{
throw new Error('Failed to get authenticated client');
}

let currentConversationId = selectedConversationId;

if (!currentConversationId) {
try {
const newConversation = await client.createConversation();
console.log('newConversation:', newConversation);

if (!newConversation || !newConversation.results) {
throw new Error('Failed to create a new conversation');
}

currentConversationId = newConversation.results;

if (typeof currentConversationId !== 'string') {
throw new Error('Invalid conversation ID received');
}

console.log('New conversation ID:', currentConversationId);
setSelectedConversationId(currentConversationId);
} catch (error) {
console.error('Error creating new conversation:', error);
setError('Failed to create a new conversation. Please try again.');
return;
}
}

if (!currentConversationId) {
setError('No valid conversation ID. Please try again.');
return;
}

console.log('Using conversation ID:', currentConversationId);

const ragGenerationConfig: GenerationConfig = {
stream: true,
temperature: rag_temperature ?? undefined,
Expand All @@ -210,17 +249,18 @@ export const Result: FC<{

const kgSearchSettings: KGSearchSettings = {
use_kg_search: switches.knowledge_graph_search?.checked ?? false,
kg_search_type: kg_search_type,
max_llm_queries_for_global_search: max_llm_queries_for_global_search,
};

const streamResponse =
mode === 'rag_agent'
? await client.agent(
[...messages, newUserMessage],
ragGenerationConfig,
vectorSearchSettings,
kgSearchSettings,
ragGenerationConfig
undefined,
undefined,
currentConversationId
)
: await client.rag(
query,
Expand All @@ -232,6 +272,8 @@ export const Result: FC<{
const reader = streamResponse.getReader();
const decoder = new TextDecoder();

let assistantResponse = '';

while (true) {
if (signal.aborted) {
reader.cancel();
Expand Down Expand Up @@ -296,6 +338,7 @@ export const Result: FC<{
}

fullContent += chunk;
assistantResponse += chunk;
updateLastMessage(
fullContent,
{ vector: vectorSearchSources, kg: kgSearchResult },
Expand All @@ -304,6 +347,28 @@ export const Result: FC<{
);
}
}

if (assistantResponse) {
updateLastMessage(
assistantResponse,
{ vector: vectorSearchSources, kg: kgSearchResult },
false,
searchPerformed
);

try {
await client.addMessage(currentConversationId, {
role: 'assistant',
content: assistantResponse,
});
console.log('Added assistant message to conversation');
} catch (error) {
console.error(
'Error adding assistant message to conversation:',
error
);
}
}
} catch (err: unknown) {
if (err instanceof Error) {
if (err.name === 'AbortError') {
Expand Down
Loading

0 comments on commit 3f4b101

Please sign in to comment.