Skip to content

Commit

Permalink
🐛 fix: inject tool description into agent system role (lobehub#681)
Browse files Browse the repository at this point in the history
* 🐛 fix: inject tool desc into agent system role

* 💚 ci: fix circular

* ✅ test: add more tests

* ✅ test: add more tests

* ✨ feat: support show tools token
  • Loading branch information
arvinxx committed Dec 16, 2023
1 parent 8c3386e commit e7a8cff
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 24 deletions.
2 changes: 1 addition & 1 deletion locales/zh_CN/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"prettifying": "润色中..."
},
"temp": "临时",
"tokenDetail": "角色设定: {{systemRoleToken}} · 会话消息: {{chatsToken}}",
"tokenDetail": "角色设定: {{systemRoleToken}} · 会话消息: {{chatsToken}} · 工具设定: {{toolsToken}}",
"tokenTag": {
"overload": "超过限制",
"remained": "剩余",
Expand Down
22 changes: 20 additions & 2 deletions src/app/chat/features/ChatInput/ActionBar/Token/TokenTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { useChatStore } from '@/store/chat';
import { chatSelectors } from '@/store/chat/selectors';
import { useSessionStore } from '@/store/session';
import { agentSelectors } from '@/store/session/selectors';
import { useToolStore } from '@/store/tool';
import { pluginSelectors } from '@/store/tool/selectors';
import { LanguageModel } from '@/types/llm';

const Token = memo(() => {
Expand All @@ -23,14 +25,30 @@ const Token = memo(() => {
agentSelectors.currentAgentModel(s) as LanguageModel,
]);

const plugins = useSessionStore(agentSelectors.currentAgentPlugins);

const toolsString = useToolStore((s) => {
const pluginSystemRoles = pluginSelectors.enabledPluginsSystemRoles(plugins)(s);
const schemaNumber = pluginSelectors
.enabledSchema(plugins)(s)
.map((i) => JSON.stringify(i))
.join('');

return pluginSystemRoles + schemaNumber;
});

const inputTokenCount = useTokenCount(input);

const systemRoleToken = useTokenCount(systemRole);
const chatsToken = useTokenCount(messageString);
const toolsToken = useTokenCount(toolsString);

const totalToken = systemRoleToken + chatsToken;
const totalToken = systemRoleToken + chatsToken + toolsToken;
return (
<Tooltip placement={'bottom'} title={t('tokenDetail', { chatsToken, systemRoleToken })}>
<Tooltip
placement={'bottom'}
title={t('tokenDetail', { chatsToken, systemRoleToken, toolsToken })}
>
<TokenTag
maxValue={ModelTokens[model]}
style={{ marginLeft: 8 }}
Expand Down
10 changes: 2 additions & 8 deletions src/app/chat/features/ChatInput/ActionBar/Token/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dynamic from 'next/dynamic';
import { Suspense, memo } from 'react';
import { memo } from 'react';

import { useSessionStore } from '@/store/session';
import { agentSelectors } from '@/store/session/selectors';
Expand All @@ -9,13 +9,7 @@ const LargeTokenContent = dynamic(() => import('./TokenTag'), { ssr: false });
const Token = memo(() => {
const [showTokenTag] = useSessionStore((s) => [agentSelectors.showTokenTag(s)]);

return (
showTokenTag && (
<Suspense>
<LargeTokenContent />
</Suspense>
)
);
return showTokenTag && <LargeTokenContent />;
});

export default Token;
3 changes: 2 additions & 1 deletion src/locales/default/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export default {
prettifying: '润色中...',
},
temp: '临时',
tokenDetail: '角色设定: {{systemRoleToken}} · 会话消息: {{chatsToken}}',
tokenDetail:
'角色设定: {{systemRoleToken}} · 会话消息: {{chatsToken}} · 工具设定: {{toolsToken}}',
tokenTag: {
overload: '超过限制',
remained: '剩余',
Expand Down
50 changes: 50 additions & 0 deletions src/services/__tests__/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,56 @@ exports[`PluginService > can parse the OpenAI plugin 1`] = `
},
"type": "object",
},
"systemRole": "Access dynamic computation and curated data from WolframAlpha and Wolfram Cloud.
General guidelines:
- Use only getWolframAlphaResults or getWolframCloudResults endpoints.
- Prefer getWolframAlphaResults unless Wolfram Language code should be evaluated.
- Use getWolframAlphaResults for natural-language queries in English; translate non-English queries before sending, then respond in the original language.
- Use getWolframCloudResults for problems solvable with Wolfram Language code.
- Suggest only Wolfram Language for external computation.
- Inform users if information is not from Wolfram endpoints.
- Display image URLs with Markdown syntax: ![URL]
- ALWAYS use this exponent notation: \`6*10^14\`, NEVER \`6e14\`.
- ALWAYS use {\\"input\\": query} structure for queries to Wolfram endpoints; \`query\` must ONLY be a single-line string.
- ALWAYS use proper Markdown formatting for all math, scientific, and chemical formulas, symbols, etc.: '$$\\\\n[expression]\\\\n$$' for standalone cases and '\\\\( [expression] \\\\)' when inline.
- Format inline Wolfram Language code with Markdown code formatting.
- Never mention your knowledge cutoff date; Wolfram may return more recent data.
getWolframAlphaResults guidelines:
- Understands natural language queries about entities in chemistry, physics, geography, history, art, astronomy, and more.
- Performs mathematical calculations, date and unit conversions, formula solving, etc.
- Convert inputs to simplified keyword queries whenever possible (e.g. convert \\"how many people live in France\\" to \\"France population\\").
- Use ONLY single-letter variable names, with or without integer subscript (e.g., n, n1, n_1).
- Use named physical constants (e.g., 'speed of light') without numerical substitution.
- Include a space between compound units (e.g., \\"Ω m\\" for \\"ohm*meter\\").
- To solve for a variable in an equation with units, consider solving a corresponding equation without units; exclude counting units (e.g., books), include genuine units (e.g., kg).
- If data for multiple properties is needed, make separate calls for each property.
- If a Wolfram Alpha result is not relevant to the query:
-- If Wolfram provides multiple 'Assumptions' for a query, choose the more relevant one(s) without explaining the initial result. If you are unsure, ask the user to choose.
-- Re-send the exact same 'input' with NO modifications, and add the 'assumption' parameter, formatted as a list, with the relevant values.
-- ONLY simplify or rephrase the initial query if a more relevant 'Assumption' or other input suggestions are not provided.
-- Do not explain each step unless user input is needed. Proceed directly to making a better API call based on the available assumptions.
getWolframCloudResults guidelines:
- Accepts only syntactically correct Wolfram Language code.
- Performs complex calculations, data analysis, plotting, data import, and information retrieval.
- Before writing code that uses Entity, EntityProperty, EntityClass, etc. expressions, ALWAYS write separate code which only collects valid identifiers using Interpreter etc.; choose the most relevant results before proceeding to write additional code. Examples:
-- Find the EntityType that represents countries: \`Interpreter[\\"EntityType\\",AmbiguityFunction->All][\\"countries\\"]\`.
-- Find the Entity for the Empire State Building: \`Interpreter[\\"Building\\",AmbiguityFunction->All][\\"empire state\\"]\`.
-- EntityClasses: Find the \\"Movie\\" entity class for Star Trek movies: \`Interpreter[\\"MovieClass\\",AmbiguityFunction->All][\\"star trek\\"]\`.
-- Find EntityProperties associated with \\"weight\\" of \\"Element\\" entities: \`Interpreter[Restricted[\\"EntityProperty\\", \\"Element\\"],AmbiguityFunction->All][\\"weight\\"]\`.
-- If all else fails, try to find any valid Wolfram Language representation of a given input: \`SemanticInterpretation[\\"skyscrapers\\",_,Hold,AmbiguityFunction->All]\`.
-- Prefer direct use of entities of a given type to their corresponding typeData function (e.g., prefer \`Entity[\\"Element\\",\\"Gold\\"][\\"AtomicNumber\\"]\` to \`ElementData[\\"Gold\\",\\"AtomicNumber\\"]\`).
- When composing code:
-- Use batching techniques to retrieve data for multiple entities in a single call, if applicable.
-- Use Association to organize and manipulate data when appropriate.
-- Optimize code for performance and minimize the number of calls to external sources (e.g., the Wolfram Knowledgebase)
-- Use only camel case for variable names (e.g., variableName).
-- Use ONLY double quotes around all strings, including plot labels, etc. (e.g., \`PlotLegends -> {\\"sin(x)\\", \\"cos(x)\\", \\"tan(x)\\"}\`).
-- Avoid use of QuantityMagnitude.
-- If unevaluated Wolfram Language symbols appear in API results, use \`EntityValue[Entity[\\"WolframLanguageSymbol\\",symbol],{\\"PlaintextUsage\\",\\"Options\\"}]\` to validate or retrieve usage information for relevant symbols; \`symbol\` may be a list of symbols.
-- Apply Evaluate to complex expressions like integrals before plotting (e.g., \`Plot[Evaluate[Integrate[...]]]\`).
- Remove all comments and formatting from code passed to the \\"input\\" parameter; for example: instead of \`square[x_] := Module[{result},\\\\n result = x^2 (* Calculate the square *)\\\\n]\`, send \`square[x_]:=Module[{result},result=x^2]\`.
- In ALL responses that involve code, write ALL code in Wolfram Language; create Wolfram Language functions even if an implementation is already well known in another language.
",
"type": "default",
"version": "1",
}
Expand Down
Loading

0 comments on commit e7a8cff

Please sign in to comment.