Skip to content

Commit

Permalink
feat: impl deepseek (#625)
Browse files Browse the repository at this point in the history
- 支持下国产大模型
  • Loading branch information
RaoHai authored Dec 27, 2024
2 parents a0125c7 + 7691eb5 commit 63c97d7
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/aws-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ env:
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
actions: write
actions: write

jobs:
deploy:
Expand Down Expand Up @@ -67,6 +67,7 @@ jobs:
ParameterKey=OpenAIAPIKey,ParameterValue=${{ secrets.OPENAI_API_KEY }} \
ParameterKey=OpenAIBaseUrl,ParameterValue=${{ vars.OPENAI_BASE_URL || 'https://api.openai.com/v1' }} \
ParameterKey=GeminiAPIKey,ParameterValue=${{ secrets.GEMINI_API_KEY }} \
ParameterKey=DeepSeekAPIKey,ParameterValue=${{ secrets.DEEPSEEK_API_KEY }} \
ParameterKey=SupabaseServiceKey,ParameterValue=${{ secrets.SUPABASE_SERVICE_KEY }} \
ParameterKey=SupabaseUrl,ParameterValue=${{ secrets.SUPABASE_URL }} \
ParameterKey=TavilyAPIKey,ParameterValue=${{ secrets.TAVILY_API_KEY }} \
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/aws-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ jobs:
ParameterKey=GithubAppsClientId,ParameterValue=${{ secrets.X_GITHUB_APPS_CLIENT_ID }} \
ParameterKey=GithubAppsClientSecret,ParameterValue=${{ secrets.X_GITHUB_APPS_CLIENT_SECRET }} \
ParameterKey=OpenAIAPIKey,ParameterValue=${{ secrets.OPENAI_API_KEY }} \
ParameterKey=DeepSeekAPIKey,ParameterValue=${{ secrets.DEEPSEEK_API_KEY }} \
ParameterKey=OpenAIBaseUrl,ParameterValue=${{ vars.OPENAI_BASE_URL || 'https://api.openai.com/v1' }} \
ParameterKey=GeminiAPIKey,ParameterValue=${{ secrets.GEMINI_API_KEY }} \
ParameterKey=SupabaseServiceKey,ParameterValue=${{ secrets.SUPABASE_SERVICE_KEY }} \
Expand Down
2 changes: 1 addition & 1 deletion client/.kiwi/en/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default {
gITHU: ' GitHub platform',
},
CreateButton: {
chuangJianTOK: 'Create Token',
chuangJianTOK: 'Custodial Token',
},
CreateModal: {
miYao: 'Secret Key',
Expand Down
2 changes: 1 addition & 1 deletion client/.kiwi/ja/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default {
gITHU: ' GitHubプラットフォーム',
},
CreateButton: {
chuangJianTOK: 'トークンを作成',
chuangJianTOK: '托管されたトークン',
},
CreateModal: {
miYao: 'シークレットキー',
Expand Down
2 changes: 1 addition & 1 deletion client/.kiwi/ko/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default {
gITHU: ' GitHub 플랫폼',
},
CreateButton: {
chuangJianTOK: '토큰 생성',
chuangJianTOK: '위탁된 토큰',
},
CreateModal: {
miYao: '비밀 키',
Expand Down
2 changes: 1 addition & 1 deletion client/.kiwi/zh-CN/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default {
gITHU: ' GitHub 平台',
},
CreateButton: {
chuangJianTOK: '创建 Token',
chuangJianTOK: '托管 Token',
},
CreateModal: {
miYao: '密钥',
Expand Down
2 changes: 1 addition & 1 deletion client/.kiwi/zh-TW/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default {
gITHU: ' GitHub 平台',
},
CreateButton: {
chuangJianTOK: '創建 Token',
chuangJianTOK: '代管 Token',
},
CreateModal: {
miYao: '密鑰',
Expand Down
10 changes: 9 additions & 1 deletion client/app/hooks/useToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useQuery } from '@tanstack/react-query';
import { getTokenList } from '../services/TokensController';
import { getTokenList, getLLMList } from '../services/TokensController';
import { Tables, Database } from '@/types/database.types';
import { Updater, useImmer } from 'use-immer';

Expand All @@ -21,3 +21,11 @@ export function useCreateToken(): [LLMTokenInsert, Updater<LLMTokenInsert>] {

return [llmToken, setLLMToken]
}

export function useListLLMs() {
return useQuery<string[]>({
queryKey: [`llm.list`],
queryFn: async () => getLLMList(),
retry: false,
});
}
7 changes: 7 additions & 0 deletions client/app/services/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export async function getTokenList() {
return response.data.data;
}

export async function getLLMList() {
const response = await axios.get(`${apiDomain}/api/user/llms`);
return response.data;
}


export async function deleteToken(id: string) {
const response = await axios.delete(`${apiDomain}/api/user/llm_token/${id}`);
return response.data;
Expand All @@ -34,3 +40,4 @@ export async function analyzeTopUsers() {
return response.data;
}


9 changes: 5 additions & 4 deletions client/app/user/tokens/components/CreateModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import I18N from '@/app/utils/I18N';
import { LLMTokenInsert, useCreateToken } from '@/app/hooks/useToken';
import { LLMTokenInsert, useCreateToken, useListLLMs } from '@/app/hooks/useToken';
import {
Button,
Input,
Expand All @@ -24,6 +24,8 @@ export interface CreateModalProps {

export default function CreateModal({ isOpen, onClose, isLoading, onCreate }: CreateModalProps) {
const [llmToken, setLLMToken] = useCreateToken();
const { data: llms = [] } = useListLLMs();

useEffect(() => setLLMToken({}), []);

const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
Expand All @@ -48,7 +50,7 @@ export default function CreateModal({ isOpen, onClose, isLoading, onCreate }: Cr
<ModalBody>
<form>
<div className="flex-1">

<div className="mb-[42px]">
<Input
name="slug"
Expand All @@ -67,8 +69,7 @@ export default function CreateModal({ isOpen, onClose, isLoading, onCreate }: Cr
required
onChange={handleChange}
>
<SelectItem key="openai">openai</SelectItem>
<SelectItem key="gemini">gemini</SelectItem>
{llms?.map(llm => <SelectItem key={llm}>{llm}</SelectItem>)}
</Select>
</div>
<div className="mt-[42px]">
Expand Down
49 changes: 49 additions & 0 deletions server/agent/llm/clients/deepseek.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@


from typing import Any, List, Optional
from langchain_openai import ChatOpenAI
from langchain_core.utils.function_calling import convert_to_openai_tool

from agent.llm import register_llm_client
from agent.llm.base import BaseLLMClient

from petercat_utils.data_class import MessageContent
from petercat_utils import get_env_variable

DEEPSEEK_API_KEY = get_env_variable("DEEPSEEK_API_KEY")


@register_llm_client("deepseek")
class DeepSeekClient(BaseLLMClient):
_client: ChatOpenAI

def __init__(
self,
temperature: Optional[float] = 0.2,
n: Optional[int] = 1,
top_p: Optional[float] = None,
max_tokens: Optional[int] = 1500,
streaming: Optional[bool] = False,
api_key: Optional[str] = DEEPSEEK_API_KEY,
):
self._client = ChatOpenAI(
model_name="deepseek-chat",
temperature=temperature,
n=n,
top_p=top_p,
streaming=streaming,
max_tokens=max_tokens,
openai_api_key=api_key,
stream_usage=True,
openai_api_base="https://api.deepseek.com"
)

def get_client(self):
return self._client

def get_tools(self, tools: List[Any]):
return [convert_to_openai_tool(tool) for tool in tools]

def parse_content(self, content: List[MessageContent]):
print(f"parse_conent, content={content}")
return content
2 changes: 1 addition & 1 deletion server/core/service/user_token_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def top_bots(self, start_date: datetime.date, end_date: datetime.date):

def top_users(self, start_date: datetime.date, end_date: datetime.date):
return self.user_token_usage_dao.top_users(start_date=start_date, end_date=end_date)

def get_user_token_usage_service():
return UserTokenUsageService()

Expand Down
17 changes: 11 additions & 6 deletions template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Parameters:
Type: String
Description: Gemini API Key
Default: 1
DeepSeekAPIKey:
Type: String
Description: DeepSeek API Key
Default: 1
SupabaseServiceKey:
Type: String
Description: Supabase Service Key
Expand Down Expand Up @@ -129,7 +133,7 @@ Resources:
PETERCAT_ENV: !Ref PetercatEnv
X_GITHUB_SECRET_NAME: !Ref GithubSecretName
STATIC_SECRET_NAME: !Ref StaticSecretName
LLM_TOKEN_SECRET_NAME: !Ref LLMTokenSecretName
LLM_TOKEN_SECRET_NAME: !Ref LLMTokenSecretName
LLM_TOKEN_PUBLIC_NAME: !Ref LLMTokenPublicName
STATIC_KEYPAIR_ID: !Ref StaticKeyPairId
S3_TEMP_BUCKET_NAME: !Ref S3TempBucketName
Expand Down Expand Up @@ -164,11 +168,11 @@ Resources:
Resource: '*'
- Sid: AllObjectActions
Effect: Allow
Action:
Action:
- s3:PutObject
- s3:GetObject
- s3:DeleteObject
Resource:
Resource:
- !Sub 'arn:aws:s3:::${S3TempBucketName}/*'
Tracing: Active
Metadata:
Expand All @@ -192,6 +196,7 @@ Resources:
FASTAPI_SECRET_KEY: !Ref FastAPISecretKey
OPENAI_API_KEY: !Ref OpenAIAPIKey
GEMINI_API_KEY: !Ref GeminiAPIKey
DEEPSEEK_API_KEY: !Ref DeepSeekAPIKey
SUPABASE_SERVICE_KEY: !Ref SupabaseServiceKey
SUPABASE_URL: !Ref SupabaseUrl
GITHUB_TOKEN: !Ref GitHubToken
Expand All @@ -211,14 +216,14 @@ Resources:
Resource: '*'
- Sid: AllObjectActions
Effect: Allow
Action:
Action:
- s3:PutObject
- s3:GetObject
- s3:DeleteObject
Resource:
Resource:
- !Sub 'arn:aws:s3:::${S3TempBucketName}/*'
- SQSPollerPolicy:
QueueName:
QueueName:
!Ref SQSQueueName
Tracing: Active
Metadata:
Expand Down

0 comments on commit 63c97d7

Please sign in to comment.