Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fn call and summary feature #730

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions .env.example

This file was deleted.

6 changes: 5 additions & 1 deletion app/(chat)/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export async function generateTitleFromUserMessage({
}

export async function deleteTrailingMessages({ id }: { id: string }) {
const [message] = await getMessageById({ id });
const message = await getMessageById({ id });

if (!message) {
throw new Error('Message not found');
}

await deleteMessagesByChatIdAfterTimestamp({
chatId: message.chatId,
Expand Down
48 changes: 22 additions & 26 deletions app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

import { auth } from '@/app/(auth)/auth';
import { customModel } from '@/lib/ai';
import { models } from '@/lib/ai/models';
import { models, DEFAULT_MODEL_NAME } from '@/lib/ai/models';
import { systemPrompt } from '@/lib/ai/prompts';
import {
deleteChatById,
Expand All @@ -18,15 +18,15 @@ import {
} from '@/lib/db/queries';
import {
generateUUID,
getAllButLastUserMessage,
getMostRecentUserMessage,
sanitizeResponseMessages,
} from '@/lib/utils';

import { generateTitleFromUserMessage } from '../../actions';
import { createDocument } from '@/lib/ai/tools/create-document';
import { updateDocument } from '@/lib/ai/tools/update-document';
import { requestSuggestions } from '@/lib/ai/tools/request-suggestions';
import { getWeather } from '@/lib/ai/tools/get-weather';
import { endConversation } from '@/lib/ai/tools/end-conversation';
import { lookupFlightManual } from '@/lib/ai/tools/lookup-flight-manual';


export const maxDuration = 60;

Expand All @@ -36,21 +36,12 @@ type AllowedTools =
| 'requestSuggestions'
| 'getWeather';

const blocksTools: AllowedTools[] = [
'createDocument',
'updateDocument',
'requestSuggestions',
];

const weatherTools: AllowedTools[] = ['getWeather'];
const allTools: AllowedTools[] = [...blocksTools, ...weatherTools];

export async function POST(request: Request) {
const {
id,
messages,
modelId,
}: { id: string; messages: Array<Message>; modelId: string } =
}: { id: string; messages: Array<Message> } =
await request.json();

const session = await auth();
Expand All @@ -59,15 +50,15 @@ export async function POST(request: Request) {
return new Response('Unauthorized', { status: 401 });
}

const model = models.find((model) => model.id === modelId);
const model = models.find((model) => model.id === DEFAULT_MODEL_NAME);

if (!model) {
return new Response('Model not found', { status: 404 });
}

const coreMessages = convertToCoreMessages(messages);
const userMessage = getMostRecentUserMessage(coreMessages);

const allButLastUserMessage = getAllButLastUserMessage(coreMessages);
if (!userMessage) {
return new Response('No user message found', { status: 400 });
}
Expand All @@ -77,6 +68,16 @@ export async function POST(request: Request) {
if (!chat) {
const title = await generateTitleFromUserMessage({ message: userMessage });
await saveChat({ id, userId: session.user.id, title });
await saveMessages({
messages: [
...allButLastUserMessage.map(message => ({
...message,
id: generateUUID(),
createdAt: new Date(),
chatId: id
})),
],
});
}

const userMessageId = generateUUID();
Expand All @@ -99,19 +100,14 @@ export async function POST(request: Request) {
system: systemPrompt,
messages: coreMessages,
maxSteps: 5,
experimental_activeTools: allTools,
// experimental_activeTools: allTools,
experimental_transform: smoothStream({ chunking: 'word' }),
tools: {
getWeather,
createDocument: createDocument({ session, dataStream, model }),
updateDocument: updateDocument({ session, dataStream, model }),
requestSuggestions: requestSuggestions({
session,
dataStream,
model,
}),
endConversation: endConversation({ dataStream }),
lookupFlightManual: lookupFlightManual({ dataStream }),
},
onFinish: async ({ response }) => {
console.log('onFinish called', response);
if (session.user?.id) {
try {
const responseMessagesWithoutIncompleteToolCalls =
Expand Down
102 changes: 0 additions & 102 deletions app/(chat)/api/document/route.ts

This file was deleted.

48 changes: 0 additions & 48 deletions app/(chat)/api/vote/route.ts

This file was deleted.

5 changes: 1 addition & 4 deletions app/(chat)/chat/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,13 @@ export default async function Page(props: { params: Promise<{ id: string }> }) {

const cookieStore = await cookies();
const modelIdFromCookie = cookieStore.get('model-id')?.value;
const selectedModelId =
models.find((model) => model.id === modelIdFromCookie)?.id ||
DEFAULT_MODEL_NAME;


return (
<>
<Chat
id={chat.id}
initialMessages={convertToUIMessages(messagesFromDb)}
selectedModelId={selectedModelId}
selectedVisibilityType={chat.visibility}
isReadonly={session?.user?.id !== chat.userId}
/>
Expand Down
11 changes: 1 addition & 10 deletions app/(chat)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { cookies } from 'next/headers';

import { Chat } from '@/components/chat';
import { DEFAULT_MODEL_NAME, models } from '@/lib/ai/models';
import { generateUUID } from '@/lib/utils';
import { DataStreamHandler } from '@/components/data-stream-handler';

export default async function Page() {
const id = generateUUID();

const cookieStore = await cookies();
const modelIdFromCookie = cookieStore.get('model-id')?.value;

const selectedModelId =
models.find((model) => model.id === modelIdFromCookie)?.id ||
DEFAULT_MODEL_NAME;

return (
<>
<Chat
key={id}
id={id}
initialMessages={[]}
selectedModelId={selectedModelId}
initialMessages={[{ id: generateUUID(), content: "Hi Mr Pilot, Please briefly describe your squawk and the aircraft type. (at any time during conversation if you think you provide all the info or it should be clear enough, you can simply type 'Exit' to end the conversation)", role: "assistant" }]}
selectedVisibilityType="private"
isReadonly={false}
/>
Expand Down
2 changes: 1 addition & 1 deletion components/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function AppSidebar({ user }: { user: User | undefined }) {
className="flex flex-row gap-3 items-center"
>
<span className="text-lg font-semibold px-2 hover:bg-muted rounded-md cursor-pointer">
Chatbot
Squawk AI
</span>
</Link>
<Tooltip>
Expand Down
Loading