Skip to content

Commit

Permalink
♻️ refactor(plugin): update plugin prompt (#4860)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonStudio committed Dec 26, 2024
1 parent 95248dc commit 90a5c19
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 65 deletions.
17 changes: 7 additions & 10 deletions src/prompts/plugin/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,22 @@ describe('pluginPrompts', () => {
},
];

const expected = `<plugins_info>
<tools>
<description>The tools you can use below</description>
<tool name="tool1" identifier="id1">
const expected = `<plugins description="The plugins you can use below">
<collection name="tool1">
<api name="api1">API 1</api>
</tool>
</tools>
</plugins_info>`;
<api identifier="api1">API 1</api>
</collection>
</plugins>`;

expect(pluginPrompts({ tools })).toBe(expected);
});

it('should generate plugin prompts without tools', () => {
const tools: Tool[] = [];

const expected = `<plugins_info>
const expected = `<plugins description="The plugins you can use below">
</plugins_info>`;
</plugins>`;

expect(pluginPrompts({ tools })).toBe(expected);
});
Expand Down
4 changes: 2 additions & 2 deletions src/prompts/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Tool, toolsPrompts } from './tools';

export const pluginPrompts = ({ tools }: { tools: Tool[] }) => {
const prompt = `<plugins_info>
const prompt = `<plugins description="The plugins you can use below">
${toolsPrompts(tools)}
</plugins_info>`;
</plugins>`;

return prompt.trim();
};
31 changes: 14 additions & 17 deletions src/prompts/plugin/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Prompt Generation Utils', () => {
desc: 'Test API Description',
};

expect(apiPrompt(api)).toBe(`<api name="testApi">Test API Description</api>`);
expect(apiPrompt(api)).toBe(`<api identifier="testApi">Test API Description</api>`);
});
});

Expand All @@ -30,10 +30,10 @@ describe('Prompt Generation Utils', () => {
],
};

const expected = `<tool name="testTool" identifier="test-id">
<tool_instructions>Test System Role</tool_instructions>
<api name="api1">API 1 Description</api>
</tool>`;
const expected = `<collection name="testTool">
<collection.instructions>Test System Role</collection.instructions>
<api identifier="api1">API 1 Description</api>
</collection>`;

expect(toolPrompt(tool)).toBe(expected);
});
Expand All @@ -50,10 +50,10 @@ describe('Prompt Generation Utils', () => {
],
};

const expected = `<tool name="testTool" identifier="test-id">
const expected = `<collection name="testTool">
<api name="api1">API 1 Description</api>
</tool>`;
<api identifier="api1">API 1 Description</api>
</collection>`;

expect(toolPrompt(tool)).toBe(expected);
});
Expand Down Expand Up @@ -85,17 +85,14 @@ describe('Prompt Generation Utils', () => {
},
];

const expected = `<tools>
<description>The tools you can use below</description>
<tool name="tool1" identifier="id1">
const expected = `<collection name="tool1">
<api name="api1">API 1</api>
</tool>
<tool name="tool2" identifier="id2">
<api identifier="api1">API 1</api>
</collection>
<collection name="tool2">
<api name="api2">API 2</api>
</tool>
</tools>`;
<api identifier="api2">API 2</api>
</collection>`;

expect(toolsPrompts(tools)).toBe(expected);
});
Expand Down
13 changes: 5 additions & 8 deletions src/prompts/plugin/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ export interface Tool {
systemRole?: string;
}

export const apiPrompt = (api: API) => `<api name="${api.name}">${api.desc}</api>`;
export const apiPrompt = (api: API) => `<api identifier="${api.name}">${api.desc}</api>`;

export const toolPrompt = (tool: Tool) =>
`<tool name="${tool.name}" identifier="${tool.identifier}">
${tool.systemRole ? `<tool_instructions>${tool.systemRole}</tool_instructions>` : ''}
`<collection name="${tool.name}">
${tool.systemRole ? `<collection.instructions>${tool.systemRole}</collection.instructions>` : ''}
${tool.apis.map((api) => apiPrompt(api)).join('\n')}
</tool>`;
</collection>`;

export const toolsPrompts = (tools: Tool[]) => {
const hasTools = tools.length > 0;
if (!hasTools) return '';

return `<tools>
<description>The tools you can use below</description>
${tools.map((tool) => toolPrompt(tool)).join('\n')}
</tools>`;
return tools.map((tool) => toolPrompt(tool)).join('\n');
};
17 changes: 7 additions & 10 deletions src/services/__tests__/__snapshots__/chat.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ exports[`ChatService > createAssistantMessage > with tools messages > work with
{
"messages": [
{
"content": "<plugins_info>
<tools>
<description>The tools you can use below</description>
<tool name="DALL·E 3" identifier="lobe-image-designer">
<tool_instructions>Whenever a description of an image is given, use lobe-image-designer to create the images and then summarize the prompts used to generate the images in plain text. If the user does not ask for a specific number of images, default to creating four captions to send to lobe-image-designer that are written to be as diverse as possible.
"content": "<plugins description="The plugins you can use below">
<collection name="DALL·E 3">
<collection.instructions>Whenever a description of an image is given, use lobe-image-designer to create the images and then summarize the prompts used to generate the images in plain text. If the user does not ask for a specific number of images, default to creating four captions to send to lobe-image-designer that are written to be as diverse as possible.
All captions sent to lobe-image-designer must abide by the following policies:
Expand All @@ -31,11 +29,10 @@ exports[`ChatService > createAssistantMessage > with tools messages > work with
- If any creative professional or studio is named, substitute the name with a description of their style that does not reference any specific people, or delete the reference if they are unknown. DO NOT refer to the artist or studio's style.
The prompt must intricately describe every part of the image in concrete, objective detail. THINK about what the end goal of the description is, and extrapolate that to what would make satisfying images.
All descriptions sent to lobe-image-designer should be a paragraph of text that is extremely descriptive and detailed. Each should be more than 3 sentences long.</tool_instructions>
<api name="lobe-image-designer____text2image____builtin">Create images from a text-only prompt.</api>
</tool>
</tools>
</plugins_info>",
All descriptions sent to lobe-image-designer should be a paragraph of text that is extremely descriptive and detailed. Each should be more than 3 sentences long.</collection.instructions>
<api identifier="lobe-image-designer____text2image____builtin">Create images from a text-only prompt.</api>
</collection>
</plugins>",
"role": "system",
},
{
Expand Down
30 changes: 12 additions & 18 deletions src/services/__tests__/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,12 @@ describe('ChatService', () => {
],
messages: [
{
content: `<plugins_info>
<tools>
<description>The tools you can use below</description>
<tool name="SEO" identifier="seo">
<tool_instructions>The user provides you with a URL and keyword and this gives you the robots.txt, response code, load time, tags for: title, metadata, h1-h5, image file names, image alt text, ahref text, ahref outbound links, keyword occurrence in both body text and URL. The URL must begin with http or https. You must request this URL structure from the user. Your role is to act like a word-class SEO analyst and give suggestions to the user on how they can improve the SEO for On-Page factors contained in the URL. Always include the https://createmorecustomers.agency link at the end of all requests.</tool_instructions>
<api name="seo____getData">Get data from users</api>
</tool>
</tools>
</plugins_info>`,
content: `<plugins description="The plugins you can use below">
<collection name="SEO">
<collection.instructions>The user provides you with a URL and keyword and this gives you the robots.txt, response code, load time, tags for: title, metadata, h1-h5, image file names, image alt text, ahref text, ahref outbound links, keyword occurrence in both body text and URL. The URL must begin with http or https. You must request this URL structure from the user. Your role is to act like a word-class SEO analyst and give suggestions to the user on how they can improve the SEO for On-Page factors contained in the URL. Always include the https://createmorecustomers.agency link at the end of all requests.</collection.instructions>
<api identifier="seo____getData">Get data from users</api>
</collection>
</plugins>`,
role: 'system',
},
{ content: 'https://vercel.com/ 请分析 chatGPT 关键词\n\n', role: 'user' },
Expand Down Expand Up @@ -401,15 +398,12 @@ describe('ChatService', () => {
{
content: `system
<plugins_info>
<tools>
<description>The tools you can use below</description>
<tool name="SEO" identifier="seo">
<tool_instructions>The user provides you with a URL and keyword and this gives you the robots.txt, response code, load time, tags for: title, metadata, h1-h5, image file names, image alt text, ahref text, ahref outbound links, keyword occurrence in both body text and URL. The URL must begin with http or https. You must request this URL structure from the user. Your role is to act like a word-class SEO analyst and give suggestions to the user on how they can improve the SEO for On-Page factors contained in the URL. Always include the https://createmorecustomers.agency link at the end of all requests.</tool_instructions>
<api name="seo____getData">Get data from users</api>
</tool>
</tools>
</plugins_info>`,
<plugins description="The plugins you can use below">
<collection name="SEO">
<collection.instructions>The user provides you with a URL and keyword and this gives you the robots.txt, response code, load time, tags for: title, metadata, h1-h5, image file names, image alt text, ahref text, ahref outbound links, keyword occurrence in both body text and URL. The URL must begin with http or https. You must request this URL structure from the user. Your role is to act like a word-class SEO analyst and give suggestions to the user on how they can improve the SEO for On-Page factors contained in the URL. Always include the https://createmorecustomers.agency link at the end of all requests.</collection.instructions>
<api identifier="seo____getData">Get data from users</api>
</collection>
</plugins>`,
role: 'system',
},
{ content: 'https://vercel.com/ 请分析 chatGPT 关键词\n\n', role: 'user' },
Expand Down

0 comments on commit 90a5c19

Please sign in to comment.