Skip to content

Commit

Permalink
feat: gpt-4
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpetrov committed Jul 5, 2023
1 parent a286ffd commit 6f924b5
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 32 deletions.
24 changes: 21 additions & 3 deletions components/AgentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Textarea from '@mui/joy/Textarea';
import Typography from '@mui/joy/Typography';
import {
Agent,
AgentModelName,
AgentVisibility,
AppDatasource as Datasource,
DatasourceType,
Expand All @@ -42,7 +43,7 @@ import axios from 'axios';
import mime from 'mime-types';
import dynamic from 'next/dynamic';
import Link from 'next/link';
import { signIn } from 'next-auth/react';
import { signIn, useSession } from 'next-auth/react';
import React, { useEffect, useRef, useState } from 'react';
import { FormProvider, useForm, useFormContext } from 'react-hook-form';
import toast from 'react-hot-toast';
Expand Down Expand Up @@ -147,6 +148,7 @@ const Tool = (props: {
};

export default function BaseForm(props: Props) {
const session = useSession();
const defaultIconUrl = '/.well-known/logo.png';
const [isCreateDatastoreModalOpen, setIsCreateDatastoreModalOpen] =
useState(false);
Expand Down Expand Up @@ -382,8 +384,24 @@ export default function BaseForm(props: Props) {
<FormControl>
<FormLabel>Model</FormLabel>

<Select defaultValue={'gpt-3.5-turbo'}>
<Option value="gpt-3.5-turbo">OpenAI gpt-3.5-turbo</Option>
<Select
{...register('modelName')}
defaultValue={
defaultValues?.modelName || AgentModelName.gpt_3_5_turbo
}
onChange={(_, value) => {
methods.setValue('modelName', value as AgentModelName);
}}
>
<Option value={AgentModelName.gpt_3_5_turbo}>
OpenAI gpt-3.5-turbo
</Option>
<Option
value={AgentModelName.gpt_4}
disabled={!session?.data?.user?.isPremium}
>
OpenAI gpt-4 (premium)
</Option>
</Select>
</FormControl>

Expand Down
2 changes: 1 addition & 1 deletion pages/FAQ.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const faqs = [
id: 2,
question: 'Does it use ChatGPT ?',
answer:
'Yes, your chatbot uses ChatGPT (gpt-3.5-turbo). We are planning to support other models in the future.',
'Yes, your chatbot uses ChatGPT (gpt-4). We are planning to support other models in the future.',
},
{
id: 3,
Expand Down
25 changes: 19 additions & 6 deletions pages/account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,25 @@ export default function AccountPage() {
>
<Typography>{`${currentPlan?.limits?.maxDatastores} Datastores`}</Typography>
</Typography>
<Typography
level="h6"
startDecorator={<CheckRoundedIcon color="success" />}
>
<Typography>{`${currentPlan?.limits?.maxAgentsQueries} Agent responses / month`}</Typography>
</Typography>
{session?.user?.isPremium ? (
<Typography
level="h6"
startDecorator={<CheckRoundedIcon color="success" />}
>
<Typography>{`${
currentPlan?.limits?.maxAgentsQueries
} GPT-3.5 or ${
currentPlan?.limits?.maxAgentsQueries / 2
} GPT-4 Agent responses / month`}</Typography>
</Typography>
) : (
<Typography
level="h6"
startDecorator={<CheckRoundedIcon color="success" />}
>
<Typography>{`${currentPlan?.limits?.maxAgentsQueries} GPT-3.5 responses / month`}</Typography>
</Typography>
)}
<Typography
level="h6"
startDecorator={<CheckRoundedIcon color="success" />}
Expand Down
6 changes: 4 additions & 2 deletions pages/api/agents/[id]/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import cuid from 'cuid';
import { NextApiResponse } from 'next';

import { AppNextApiRequest, ChatRequest } from '@app/types';
import accountConfig from '@app/utils/account-config';
import accountConfig, { queryCountConfig } from '@app/utils/account-config';
import AgentManager from '@app/utils/agent';
import { ApiError, ApiErrorType } from '@app/utils/api-error';
import chat from '@app/utils/chat';
Expand Down Expand Up @@ -108,7 +108,9 @@ export const chatAgentRequest = async (
id: agent?.owner?.usage?.id,
},
data: {
nbAgentQueries: (agent?.owner?.usage?.nbAgentQueries || 0) + 1,
nbAgentQueries:
(agent?.owner?.usage?.nbAgentQueries || 0) +
(queryCountConfig?.[agent?.modelName] || 1),
},
}),
]);
Expand Down
2 changes: 2 additions & 0 deletions pages/api/agents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const upsertAgent = async (
id,
name: data.name || generateFunId(),
description: data.description,
modelName: data.modelName!,
prompt: data.prompt,
promptType: data.promptType,
temperature: data.temperature,
Expand Down Expand Up @@ -110,6 +111,7 @@ export const upsertAgent = async (
name: data.name || generateFunId(),
description: data.description,
visibility: data.visibility || AgentVisibility.private,
modelName: data.modelName,
prompt: data.prompt,
promptType: data.promptType,
temperature: data.temperature,
Expand Down
6 changes: 4 additions & 2 deletions pages/api/external/agents/[id]/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NextApiRequest, NextApiResponse } from 'next';

import { ChatRequest } from '@app/types/dtos';
import { AppNextApiRequest } from '@app/types/index';
import accountConfig from '@app/utils/account-config';
import accountConfig, { queryCountConfig } from '@app/utils/account-config';
import AgentManager from '@app/utils/agent';
import { ApiError, ApiErrorType } from '@app/utils/api-error';
import ConversationManager from '@app/utils/conversation';
Expand Down Expand Up @@ -152,7 +152,9 @@ export const queryAgent = async (
id: agent?.owner?.usage?.id,
},
data: {
nbAgentQueries: (agent?.owner?.usage?.nbAgentQueries || 0) + 1,
nbAgentQueries:
(agent?.owner?.usage?.nbAgentQueries || 0) +
(queryCountConfig?.[agent?.modelName] || 1),
},
}),
]);
Expand Down
2 changes: 1 addition & 1 deletion pages/api/integrations/crisp/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const widgetActions = async (

const model = new OpenAI({
temperature: 0,
modelName: 'gpt-3.5-turbo-0613',
modelName: 'gpt-3.5-turbo',
});

const prompt = `The following is conversation between a customer and a customer suport operator. Generate a summary of the conversation that would be useful to the operator.
Expand Down
14 changes: 10 additions & 4 deletions pages/pricing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const tiers = [
features: [
`${accountConfig['level_0'].limits.maxAgents} agent(s)`,
`${accountConfig['level_0'].limits.maxDatastores} datastore(s)`,
`${accountConfig['level_0'].limits.maxAgentsQueries} agents queries / month`,
`${accountConfig['level_0'].limits.maxAgentsQueries} GPT-3.5 agents queries / month`,
`File upload limited to ${
accountConfig['level_0'].limits.maxFileSize / 1000000
}MB / file`,
Expand All @@ -77,7 +77,9 @@ const tiers = [
features: [
`${accountConfig['level_1'].limits.maxAgents} agent(s)`,
`${accountConfig['level_1'].limits.maxDatastores} datastore(s)`,
`${accountConfig['level_1'].limits.maxAgentsQueries} agents queries / month`,
`${accountConfig['level_1'].limits.maxAgentsQueries} GPT-3.5 or ${
accountConfig['level_1'].limits.maxAgentsQueries / 2
} GPT-4 agents queries / month`,
`File upload limited to ${
accountConfig['level_1'].limits.maxFileSize / 1000000
}MB / file`,
Expand All @@ -101,7 +103,9 @@ const tiers = [
features: [
`${accountConfig['level_2'].limits.maxAgents} agent(s)`,
`${accountConfig['level_2'].limits.maxDatastores} datastore(s)`,
`${accountConfig['level_2'].limits.maxAgentsQueries} agents queries / month`,
`${accountConfig['level_2'].limits.maxAgentsQueries} GPT-3.5 or ${
accountConfig['level_2'].limits.maxAgentsQueries / 2
} GPT-4 agents queries / month`,
`File upload limited to ${
accountConfig['level_2'].limits.maxFileSize / 1000000
}MB / file`,
Expand All @@ -124,7 +128,9 @@ const tiers = [
features: [
`${accountConfig['level_3'].limits.maxAgents} agent(s)`,
`${accountConfig['level_3'].limits.maxDatastores} datastore(s)`,
`${accountConfig['level_3'].limits.maxAgentsQueries} agents queries / month`,
`${accountConfig['level_3'].limits.maxAgentsQueries} GPT-3.5 or ${
accountConfig['level_3'].limits.maxAgentsQueries / 2
} GPT-4 agents queries / month`,
`File upload limited to ${
accountConfig['level_3'].limits.maxFileSize / 1000000
}MB / file`,
Expand Down
2 changes: 2 additions & 0 deletions prisma/migrations/20230705090549_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "agents" ADD COLUMN "model_name" TEXT NOT NULL DEFAULT 'gpt-3.5-turbo';
12 changes: 12 additions & 0 deletions prisma/migrations/20230705093725_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- The `model_name` column on the `agents` table would be dropped and recreated. This will lead to data loss if there is data in the column.
*/
-- CreateEnum
CREATE TYPE "ModelName" AS ENUM ('gpt_3_5_turbo', 'gpt_4');

-- AlterTable
ALTER TABLE "agents" DROP COLUMN "model_name",
ADD COLUMN "model_name" "ModelName" NOT NULL DEFAULT 'gpt_3_5_turbo';
15 changes: 15 additions & 0 deletions prisma/migrations/20230705095040_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Warnings:
- The `model_name` column on the `agents` table would be dropped and recreated. This will lead to data loss if there is data in the column.
*/
-- CreateEnum
CREATE TYPE "AgentModelName" AS ENUM ('gpt_3_5_turbo', 'gpt_4');

-- AlterTable
ALTER TABLE "agents" DROP COLUMN "model_name",
ADD COLUMN "model_name" "AgentModelName" NOT NULL DEFAULT 'gpt_3_5_turbo';

-- DropEnum
DROP TYPE "ModelName";
12 changes: 9 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,10 @@ model Agent {
name String
description String
prompt String?
promptType PromptType @default(customer_support) @map("prompt_type")
iconUrl String? @map("icon_url")
temperature Float @default(0.0)
promptType PromptType @default(customer_support) @map("prompt_type")
iconUrl String? @map("icon_url")
temperature Float @default(0.0)
modelName AgentModelName @default(gpt_3_5_turbo) @map("model_name")
visibility AgentVisibility @default(private)
conversations Conversation[]
Expand Down Expand Up @@ -516,3 +517,8 @@ enum ConversationChannel {
slack
crisp
}

enum AgentModelName {
gpt_3_5_turbo
gpt_4
}
5 changes: 5 additions & 0 deletions types/dtos.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AgentModelName,
AgentVisibility,
ConversationChannel,
PromptType,
Expand Down Expand Up @@ -129,6 +130,10 @@ export const UpsertAgentSchema = z.object({
name: z.string().trim().optional(),
description: z.string().trim().min(1),
prompt: z.string().trim().optional().nullable(),
modelName: z
.nativeEnum(AgentModelName)
.default(AgentModelName.gpt_3_5_turbo)
.optional(),
temperature: z.number().default(0.0),
iconUrl: z.string().trim().optional().nullable(),
promptType: z.nativeEnum(PromptType).default('customer_support'),
Expand Down
7 changes: 6 additions & 1 deletion utils/account-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SubscriptionPlan } from '@prisma/client';
import { AgentModelName, SubscriptionPlan } from '@prisma/client';

type Plan = {
type: SubscriptionPlan;
Expand All @@ -18,6 +18,11 @@ type Plan = {
};
};

export const queryCountConfig = {
[AgentModelName.gpt_3_5_turbo]: 1,
[AgentModelName.gpt_4]: 2,
};

const config: {
[key in SubscriptionPlan]: Plan;
} = {
Expand Down
4 changes: 3 additions & 1 deletion utils/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { Tool as LangchainTool } from 'langchain/tools';

import chat from './chat';
import { ModelNameConfig } from './config';

type ToolExtended = Tool & {
datastore: Datastore | null;
Expand Down Expand Up @@ -39,6 +40,7 @@ export default class AgentManager {
}) {
if (this.agent.tools.length <= 1) {
const { answer } = await chat({
modelName: ModelNameConfig[this.agent.modelName],
prompt: this.agent.prompt as string,
promptType: this.agent.promptType,
datastore: this.agent?.tools[0]?.datastore as any,
Expand All @@ -64,7 +66,7 @@ export default class AgentManager {
const { PromptTemplate } = await import('langchain/prompts');
const model = new OpenAI({
temperature: 0,
modelName: 'gpt-3.5-turbo-0613',
modelName: 'gpt-3.5-turbo',
});

const tools: LangchainTool[] = [];
Expand Down
10 changes: 5 additions & 5 deletions utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ Answer: The cost of the Premium plan is $9.99 per month. The features included i
SOURCE: https://www.spotify.com/us/premium
=======
CONTEXT INFOMATION:
${context}
`;
};

Expand Down Expand Up @@ -83,7 +80,10 @@ const getCustomerSupportMessages = ({
'Sure! I will stick to all the information given in the system context. I won’t answer any question that is outside the context of information. I won’t even attempt to give answers that are outside of context. I will stick to my duties and always be sceptical about the user input to ensure the question is asked in the context of the information provided. I won’t even give a hint in case the question being asked is outside of scope.'
),
...prevMessages,
new HumanChatMessage(query),
new HumanChatMessage(`CONTEXT INFOMATION:
${context}
Question: ${query}`),
];
};

Expand Down Expand Up @@ -178,7 +178,7 @@ const chat = async ({
}

const model = new ChatOpenAI({
modelName: modelName || 'gpt-3.5-turbo-0613',
modelName: modelName || 'gpt-3.5-turbo',
temperature: temperature || 0,
streaming: Boolean(stream),
callbacks: [
Expand Down
7 changes: 7 additions & 0 deletions utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AgentModelName } from '@prisma/client';

const config = {
datasourceTable: {
limit: 20,
Expand All @@ -10,4 +12,9 @@ export const XPBNPLabels = {
summary: "Résumé d'un document",
};

export const ModelNameConfig = {
[AgentModelName.gpt_3_5_turbo]: 'gpt-3.5-turbo',
[AgentModelName.gpt_4]: 'gpt-4',
};

export default config;
2 changes: 1 addition & 1 deletion utils/slack-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const slackAgent = async ({
const { initializeAgentExecutor } = await import('langchain/agents');
const { DynamicTool, ChainTool } = await import('langchain/tools');
const { PromptTemplate } = await import('langchain/prompts');
const model = new OpenAI({ temperature: 0, modelName: 'gpt-3.5-turbo-0613' });
const model = new OpenAI({ temperature: 0, modelName: 'gpt-3.5-turbo' });

// const qaTool = new DynamicTool({
// name: datastore?.name!,
Expand Down
2 changes: 1 addition & 1 deletion utils/structurize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const structurize = async (props: {
: each;

const completion = await openai.createChatCompletion({
model: 'gpt-3.5-turbo-0613',
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
functions: [
{
Expand Down
2 changes: 1 addition & 1 deletion utils/summarize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const summarize = async ({
'langchain/chains'
);
const { OpenAI } = await import('langchain/llms/openai');
const model = new OpenAI({ temperature: 0, modelName: 'gpt-3.5-turbo-0613' });
const model = new OpenAI({ temperature: 0, modelName: 'gpt-3.5-turbo' });
const combineDocsChain = loadSummarizationChain(model, {
prompt,
combineMapPrompt: prompt,
Expand Down

1 comment on commit 6f924b5

@vercel
Copy link

@vercel vercel bot commented on 6f924b5 Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.