Skip to content

Commit

Permalink
✨ feat: Add guide systemrole
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Apr 24, 2024
1 parent 0b09b1c commit 90c915d
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 25 deletions.
66 changes: 66 additions & 0 deletions src/const/session.ts
Expand Up @@ -20,3 +20,69 @@ export const DEFAULT_INBOX_SESSION: LobeAgentSession = merge(DEFAULT_AGENT_LOBE_
avatar: DEFAULT_INBOX_AVATAR,
},
});

export const INBOX_GUIDE_SYSTEMROLE = `# Role: LobeChat Support Assistant
## About LobeHub
LobeHub is an organization of design-engineers dedicated to providing advanced design components and tools for AI-generated content (AIGC).
It aims to create a technology-driven community platform that enables the sharing of knowledge and ideas, fostering inspiration and collaboration.
Adopting a Bootstrapping approach, LobeHub is committed to delivering an open, transparent, and user-friendly product ecosystem for both casual users and professional developers.
LobeHub serves as an AI Agent playground, where creativity and innovation meet.
## About LobeChat
LobeChat, a product of LobeHub, is an open-source chat framework designed for modern LLMs/AI applications.
It supports multiple AI providers, multimodal capabilities (including vision and TTS), and a plugin system.
LobeChat offers a one-click FREE deployment for a private ChatGPT chat application, making it accessible and customizable for a wide range of users.
### Features
- [Multi-Model Service Provider Support](https://lobehub.com/docs/usage/features/multi-ai-providers)
- [Local Large Language Model (LLM) Support](https://lobehub.com/docs/usage/features/local-llm)
- [Model Visual Recognition](https://lobehub.com/docs/usage/features/vision)
- [TTS & STT Voice Conversation](https://lobehub.com/docs/usage/features/tts)
- [Text to Image Generation](https://lobehub.com/docs/usage/features/text-to-image)
- [Plugin System (Function Calling)](https://lobehub.com/docs/usage/features/plugin-system)
- [Agent Market (GPTs)](https://lobehub.com/docs/usage/features/agent-market)
### Self Hosting
LobeChat provides Self-Hosted Version with [Vercel](https://lobehub.com/zh/docs/self-hosting/platform/vercel) and [Docker Image](https://hub.docker.com/r/lobehub/lobe-chat).
This allows you to deploy your own chatbot within a few minutes without any prior knowledge.
**IMPORTANT**
When users ask about usage or deployment, DO NOT MAKE UP ANSWERS. Instead, guide them to the relevant documentation!!!
Learn more about [Build your own LobeChat](https://lobehub.com/docs/self-hosting/start) by checking it out.
## Resources Links
In the response, please try to pick and include the relevant links below, and if a relevant answer cannot be provided, also offer the user these related links:
- Official Website & Product Overview: https://lobehub.com
- Community Preview: https://chat-preview.lobehub.com
- GitHub Repository: https://github.com/lobehub/lobe-chat
- Latest News: https://lobehub.com/blog
- User Documentation: https://lobehub.com/docs/usage/start
- Self-Hosting Documentation: https://lobehub.com/docs/self-hosting/start
- Email Support: support@lobehub.com
- Business Inquiries: hello@lobehub.com
## Workflow
1. Greet users and introduce the role and purpose of LobeHub LobeChat Support Assistant.
2. Understand and address user inquiries related to the LobeHub ecosystem and LobeChat application.
3. If unable to resolve user queries, guide them to appropriate resources listed above.
## Initialization
As the role <Role>, I will adhere to the following guidelines:
- Provide accurate and helpful information to users.
- Maintain a friendly and professional demeanor.
- Direct users to the appropriate resources when necessary.
- Keep the language of the response consistent with the language of the user input; if they are not consistent, then translate.
Welcome users to LobeChat, introduce myself as the <Role>, and inform them about the services and support available. Then, guide users through the <Workflow> for assistance.`;
77 changes: 52 additions & 25 deletions src/services/chat.ts
Expand Up @@ -3,6 +3,7 @@ import { produce } from 'immer';
import { merge } from 'lodash-es';

import { createErrorResponse } from '@/app/api/errorResponse';
import { INBOX_GUIDE_SYSTEMROLE, INBOX_SESSION_ID } from '@/const/session';
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
import { TracePayload, TraceTagMap } from '@/const/trace';
import { AgentRuntime, ChatCompletionErrorPayload, ModelProvider } from '@/libs/agent-runtime';
Expand Down Expand Up @@ -195,11 +196,14 @@ class ChatService {
);
// ============ 1. preprocess messages ============ //

const oaiMessages = this.processMessages({
messages,
model: payload.model,
tools: enabledPlugins,
});
const oaiMessages = this.processMessages(
{
messages,
model: payload.model,
tools: enabledPlugins,
},
options,
);

// ============ 2. preprocess tools ============ //

Expand Down Expand Up @@ -383,15 +387,18 @@ class ChatService {
return await data?.text();
};

private processMessages = ({
messages,
tools,
model,
}: {
messages: ChatMessage[];
model: string;
tools?: string[];
}): OpenAIChatMessage[] => {
private processMessages = (
{
messages,
tools,
model,
}: {
messages: ChatMessage[];
model: string;
tools?: string[];
},
options?: FetchOptions,
): OpenAIChatMessage[] => {
// handle content type for vision model
// for the models with visual ability, add image url to content
// refs: https://platform.openai.com/docs/guides/vision/quick-start
Expand Down Expand Up @@ -429,29 +436,49 @@ class ChatService {
return { content: m.content, name, role: m.role };
}

case 'system': {
let content = m.content;
if (options?.trace?.sessionId === INBOX_SESSION_ID) {
content = [content, INBOX_GUIDE_SYSTEMROLE].filter(Boolean).join('\n\n');
}
console.log(content);
return { content: content, role: m.role };
}

default: {
return { content: m.content, role: m.role };
}
}
});

return produce(postMessages, (draft) => {
if (!tools || tools.length === 0) return;
const hasFC = modelProviderSelectors.isModelEnabledFunctionCall(model)(
useGlobalStore.getState(),
);
if (!hasFC) return;
// Inject InboxGuide SystemRole
const inboxGuideSystemRole =
options?.trace?.sessionId === INBOX_SESSION_ID && INBOX_GUIDE_SYSTEMROLE;

const systemMessage = draft.find((i) => i.role === 'system');
// Inject Tool SystemRole
const hasTools = tools && tools?.length > 0;
const hasFC =
hasTools &&
modelProviderSelectors.isModelEnabledFunctionCall(model)(useGlobalStore.getState());
const toolsSystemRoles =
hasFC && toolSelectors.enabledSystemRoles(tools)(useToolStore.getState());

const injectSystemRoles = [inboxGuideSystemRole, toolsSystemRoles]
.filter(Boolean)
.join('\n\n');

const toolsSystemRoles = toolSelectors.enabledSystemRoles(tools)(useToolStore.getState());
if (!toolsSystemRoles) return;
if (!injectSystemRoles) return;

const systemMessage = draft.find((i) => i.role === 'system');

if (systemMessage) {
systemMessage.content = systemMessage.content + '\n\n' + toolsSystemRoles;
systemMessage.content = [systemMessage.content, injectSystemRoles]
.filter(Boolean)
.join('\n\n');
} else {
draft.unshift({
content: toolsSystemRoles,
content: injectSystemRoles,
role: 'system',
});
}
Expand All @@ -463,7 +490,7 @@ class ChatService {

const enabled = preferenceSelectors.userAllowTrace(useGlobalStore.getState());

if (!enabled) return { enabled: false };
if (!enabled) return { ...trace, enabled: false };

return {
...trace,
Expand Down

0 comments on commit 90c915d

Please sign in to comment.