Skip to content

Commit 3e4f6ac

Browse files
authored
Merge pull request #16 from Dogtiti/feature/custom-prompts-language
feat: add custom language for prompts
2 parents bee2c48 + 6bd1e12 commit 3e4f6ac

File tree

6 files changed

+54
-41
lines changed

6 files changed

+54
-41
lines changed

src/components/AutonomousAgent.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ class AutonomousAgent {
5454
}
5555
} catch (e) {
5656
console.log(e);
57-
// this.sendErrorMessage(
58-
// this.modelSettings.customApiKey !== ""
59-
// ? "errors.run-with-filled-customApiKey"
60-
// : "errors.run-with-empty-customApiKey"
61-
// );
6257
this.sendErrorMessage(getMessageFromError(e));
6358
this.shutdown();
6459
return;

src/components/Drawer.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
FaQq,
1717
FaGlobe,
1818
} from "react-icons/fa";
19-
import { BiPlus } from "react-icons/bi";
2019
import clsx from "clsx";
2120
import { useAuth } from "../hooks/useAuth";
2221
import type { Session } from "next-auth";
@@ -32,12 +31,14 @@ const Drawer = ({
3231
showWeChat,
3332
showQQ,
3433
showKnowledgePlanet,
34+
handleLanguageChange,
3535
}: {
3636
showHelp: () => void;
3737
showSettings: () => void;
3838
showWeChat: () => void;
3939
showQQ: () => void;
4040
showKnowledgePlanet: () => void;
41+
handleLanguageChange: () => void;
4142
}) => {
4243
const [showDrawer, setShowDrawer] = useState(false);
4344
const { session, signIn, signOut, status } = useAuth();
@@ -75,12 +76,6 @@ const Drawer = ({
7576
setShowDrawer((prevState) => !prevState);
7677
};
7778

78-
const onToggleLanguageClick = () => {
79-
const { pathname, asPath, query, locale } = router;
80-
router.push({ pathname, query }, asPath, {
81-
locale: locale === "en" ? "zh" : "en",
82-
});
83-
};
8479
const userAgents = query.data ?? [];
8580

8681
return (
@@ -180,7 +175,7 @@ const Drawer = ({
180175
<DrawerItem
181176
icon={<FaLanguage />}
182177
text="language"
183-
onClick={onToggleLanguageClick}
178+
onClick={handleLanguageChange}
184179
/>
185180
{env.NEXT_PUBLIC_FF_SUB_ENABLED ||
186181
(router.query.pro && (

src/pages/index.tsx

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef } from "react";
1+
import React, { useEffect, useRef, useState } from "react";
22
import { type NextPage, type GetStaticProps } from "next";
33
import DefaultLayout from "../layout/default";
44
import ChatWindow from "../components/ChatWindow";
@@ -22,32 +22,35 @@ import WeChatPayDialog from "../components/WeChatPayDialog";
2222
import QQDialog from "../components/QQDialog";
2323
import KnowlegePlanetDialog from "../components/KnowlegePlanetDialog";
2424
import { useTranslation } from "next-i18next";
25+
import { useRouter } from "next/router";
2526

2627
const Home: NextPage = () => {
2728
const { session, status } = useAuth();
28-
const [name, setName] = React.useState<string>("");
29-
const [goalInput, setGoalInput] = React.useState<string>("");
30-
const [agent, setAgent] = React.useState<AutonomousAgent | null>(null);
31-
const [customApiKey, setCustomApiKey] = React.useState<string>("");
32-
const [customModelName, setCustomModelName] =
33-
React.useState<string>(GPT_35_TURBO);
34-
const [customTemperature, setCustomTemperature] = React.useState<number>(0.9);
35-
const [customMaxLoops, setCustomMaxLoops] = React.useState<number>(
29+
const [name, setName] = useState<string>("");
30+
const [goalInput, setGoalInput] = useState<string>("");
31+
const [agent, setAgent] = useState<AutonomousAgent | null>(null);
32+
const [customApiKey, setCustomApiKey] = useState<string>("");
33+
const [customModelName, setCustomModelName] = useState<string>(GPT_35_TURBO);
34+
const [customTemperature, setCustomTemperature] = useState<number>(0.9);
35+
const [customMaxLoops, setCustomMaxLoops] = useState<number>(
3636
DEFAULT_MAX_LOOPS_FREE
3737
);
38-
const [shouldAgentStop, setShouldAgentStop] = React.useState(false);
39-
40-
const [messages, setMessages] = React.useState<Message[]>([]);
41-
42-
const [showHelpDialog, setShowHelpDialog] = React.useState(false);
43-
const [showSettingsDialog, setShowSettingsDialog] = React.useState(false);
44-
const [hasSaved, setHasSaved] = React.useState(false);
45-
const [showWeChatDialog, setShowWeChatDialog] = React.useState(false);
46-
const [showWeChatPayDialog, setShowWeChatPayDialog] = React.useState(false);
47-
const [showQQDialog, setShowQQDialog] = React.useState(false);
38+
const [shouldAgentStop, setShouldAgentStop] = useState(false);
39+
const [messages, setMessages] = useState<Message[]>([]);
40+
const [showHelpDialog, setShowHelpDialog] = useState(false);
41+
const [showSettingsDialog, setShowSettingsDialog] = useState(false);
42+
const [hasSaved, setHasSaved] = useState(false);
43+
const [showWeChatDialog, setShowWeChatDialog] = useState(false);
44+
const [showWeChatPayDialog, setShowWeChatPayDialog] = useState(false);
45+
const [showQQDialog, setShowQQDialog] = useState(false);
4846
const [showKnowlegePlanetDialog, setShowKnowlegePlanetDialog] =
49-
React.useState(false);
50-
const { t } = useTranslation();
47+
useState(false);
48+
const { t, i18n } = useTranslation();
49+
const [customLanguage, setCustomLanguage] = React.useState<string>(
50+
i18n.language
51+
);
52+
53+
const router = useRouter();
5154

5255
const agentUtils = useAgent();
5356

@@ -90,7 +93,13 @@ const Home: NextPage = () => {
9093
goalInput,
9194
handleAddMessage,
9295
() => setAgent(null),
93-
{ customApiKey, customModelName, customTemperature, customMaxLoops },
96+
{
97+
customApiKey,
98+
customModelName,
99+
customTemperature,
100+
customMaxLoops,
101+
customLanguage,
102+
},
94103
session ?? undefined
95104
);
96105
setAgent(agent);
@@ -110,6 +119,15 @@ const Home: NextPage = () => {
110119
agent?.stopAgent();
111120
};
112121

122+
const handleLanguageChange = () => {
123+
const { pathname, asPath, query, locale } = router;
124+
const lng = locale === "en" ? "zh" : "en";
125+
router.push({ pathname, query }, asPath, {
126+
locale: lng,
127+
});
128+
setCustomLanguage(lng);
129+
};
130+
113131
const proTitle = (
114132
<>
115133
AutoGPT Next Web<span className="ml-1 text-amber-500/90">Pro</span>
@@ -162,6 +180,7 @@ const Home: NextPage = () => {
162180
showWeChat={() => setShowWeChatDialog(true)}
163181
showQQ={() => setShowQQDialog(true)}
164182
showKnowledgePlanet={() => setShowKnowlegePlanetDialog(true)}
183+
handleLanguageChange={handleLanguageChange}
165184
/>
166185
<div
167186
id="content"

src/services/agent-service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function startGoalAgent(modelSettings: ModelSettings, goal: string) {
1515
prompt: startGoalPrompt,
1616
}).call({
1717
goal,
18+
customLanguage: modelSettings.customLanguage,
1819
});
1920
console.log("Completion:" + (completion.text as string));
2021
return extractTasks(completion.text as string, []);
@@ -31,6 +32,7 @@ async function executeTaskAgent(
3132
}).call({
3233
goal,
3334
task,
35+
customLanguage: modelSettings.customLanguage,
3436
});
3537

3638
return completion.text as string;
@@ -52,6 +54,7 @@ async function createTasksAgent(
5254
tasks,
5355
lastTask,
5456
result,
57+
customLanguage: modelSettings.customLanguage,
5558
});
5659

5760
return extractTasks(completion.text as string, completedTasks || []);

src/utils/prompts.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ export const createModel = (settings: ModelSettings) =>
1717

1818
export const startGoalPrompt = new PromptTemplate({
1919
template:
20-
"You are an autonomous task creation AI called AgentGPT. You have the following objective `{goal}`. Create a list of zero to three tasks to be completed by your AI system such that your goal is more closely reached or completely reached. Return the response as an array of strings that can be used in JSON.parse()",
21-
inputVariables: ["goal"],
20+
"You are an autonomous task creation AI called AgentGPT. You have the following objective `{goal}`. Create a list of zero to three tasks to be completed by your AI system such that your goal is more closely reached or completely reached. Use the `{customLanguage}` language and respond only with the Array which has the following syntax:`[1...3 tasks on the given language]`",
21+
inputVariables: ["goal", "customLanguage"],
2222
});
2323

2424
export const executeTaskPrompt = new PromptTemplate({
2525
template:
26-
"You are an autonomous task execution AI called AgentGPT. You have the following objective `{goal}`. You have the following tasks `{task}`. Execute the task and return the response as a string.",
27-
inputVariables: ["goal", "task"],
26+
"You are an autonomous task execution AI called AgentGPT. You have the following objective `{goal}`. You have the following tasks `{task}`.You have to use the `{customLanguage}` language. Execute the given task and respond only with a one-line text as solution for the given task on the given language.",
27+
inputVariables: ["goal", "task", "customLanguage"],
2828
});
2929

3030
export const createTasksPrompt = new PromptTemplate({
3131
template:
32-
"You are an AI task creation agent. You have the following objective `{goal}`. You have the following incomplete tasks `{tasks}` and have just executed the following task `{lastTask}` and received the following result `{result}`. Based on this, create a new task to be completed by your AI system ONLY IF NEEDED such that your goal is more closely reached or completely reached. Return the response as an array of strings that can be used in JSON.parse() and NOTHING ELSE",
33-
inputVariables: ["goal", "tasks", "lastTask", "result"],
32+
"You are an AI task creation agent and your objective is to `{goal}`. You have the following incomplete tasks `{tasks}` and have just executed the following task `{lastTask}` and received the following solution `{result}`. Based on this, create a new task to be completed by your AI system ONLY IF NEEDED such that your goal is more closely reached or completely reached. Use the `{customLanguage}` language to create the new task. Respond only with an Array of the new task or tasks which has the following syntax:`[the remaining and appropriate new task or tasks on the language you have to use]`",
33+
inputVariables: ["goal", "tasks", "lastTask", "result", "customLanguage"],
3434
});

src/utils/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export type ModelSettings = {
33
customModelName: string;
44
customTemperature: number;
55
customMaxLoops: number;
6+
customLanguage: string;
67
};

0 commit comments

Comments
 (0)