-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- local llm support - image file without ext support - v0.3.0
- Loading branch information
1 parent
e1bb5fb
commit 7dd5dda
Showing
41 changed files
with
1,693 additions
and
408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,3 @@ | |
|
||
|
||
/temp | ||
/trails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ jieba>=0.42.1 | |
starlette>=0.37.2 | ||
jinja2~=3.1.3 | ||
apscheduler~=3.10.4 | ||
cryptography~=41.0.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[metadata] | ||
name = retk | ||
version = 0.2.9 | ||
version = 0.3.0 | ||
author = MorvanZhou | ||
author_email = [email protected] | ||
description = keep and reuse your thoughts | ||
|
@@ -44,6 +44,7 @@ install_requires = | |
starlette>=0.27.0 | ||
jinja2~=3.1.3 | ||
apscheduler~=3.10.4 | ||
cryptography~=41.0.3 | ||
|
||
[options.packages.find] | ||
where = src | ||
|
@@ -54,6 +55,8 @@ retk = | |
.env.local | ||
models/search_engine/*.txt | ||
plugins/official_plugins/**/* | ||
core/ai/llm/knowledge/*.md | ||
|
||
[options.extras_require] | ||
build = | ||
tox==3.24.3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
from retk import const, local_manager | ||
from retk.config import is_local_db | ||
from retk.controllers import schemas | ||
from retk.controllers.utils import json_exception, maybe_raise_json_exception | ||
from retk.core.ai.llm.api import LLM_DEFAULT_SERVICES | ||
from retk.models.tps import AuthedUser | ||
|
||
|
||
async def get_llm_api_settings( | ||
au: AuthedUser, | ||
) -> schemas.ai.LLMApiSettingsResponse: | ||
if not is_local_db(): | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.NOT_PERMITTED, | ||
language=au.language, | ||
) | ||
llm_settings = local_manager.llm.get_llm_api_settings() | ||
if llm_settings is None: | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.INVALID_SETTING, | ||
language=au.language, | ||
) | ||
return schemas.ai.LLMApiSettingsResponse( | ||
requestId=au.request_id, | ||
service=llm_settings.get("service", ""), | ||
model=llm_settings.get("model", ""), | ||
auth=llm_settings.get("auth", {}), | ||
) | ||
|
||
|
||
async def change_llm_api_settings( | ||
au: AuthedUser, | ||
req: schemas.ai.LLMApiSettingsRequest, | ||
) -> schemas.ai.LLMApiSettingsResponse: | ||
if not is_local_db(): | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.NOT_PERMITTED, | ||
language=au.language, | ||
) | ||
try: | ||
local_manager.llm.change_llm_api( | ||
llm_service=req.service, | ||
llm_model=req.model, | ||
llm_api_auth=req.auth, | ||
) | ||
except ValueError as e: | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.INVALID_SETTING, | ||
language=au.language, | ||
log_msg=str(e), | ||
) | ||
return schemas.ai.LLMApiSettingsResponse( | ||
requestId=au.request_id, | ||
service=req.service, | ||
model=req.model, | ||
auth=req.auth, | ||
) | ||
|
||
|
||
async def delete_llm_api_settings( | ||
au: AuthedUser, | ||
) -> schemas.RequestIdResponse: | ||
if not is_local_db(): | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.NOT_PERMITTED, | ||
language=au.language, | ||
) | ||
local_manager.llm.delete_llm_api() | ||
return schemas.RequestIdResponse( | ||
requestId=au.request_id, | ||
) | ||
|
||
|
||
async def llm_api_test( | ||
au: AuthedUser, | ||
) -> schemas.RequestIdResponse: | ||
if not is_local_db(): | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.NOT_PERMITTED, | ||
language=au.language, | ||
) | ||
llm_api = local_manager.llm.get_llm_api_settings() | ||
if llm_api is None: | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.INVALID_SETTING, | ||
language=au.language, | ||
log_msg="llm_api is None", | ||
) | ||
service = llm_api.get("service") | ||
if service not in LLM_DEFAULT_SERVICES: | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.INVALID_SETTING, | ||
language=au.language, | ||
log_msg=f"{service} not in LLM_DEFAULT_SERVICES: {LLM_DEFAULT_SERVICES.keys()}", | ||
) | ||
model = llm_api.get("model") | ||
if model is None: | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.INVALID_SETTING, | ||
language=au.language, | ||
log_msg=f"model is None", | ||
) | ||
auth = llm_api.get("auth") | ||
if auth is None or len(auth) == 0: | ||
raise json_exception( | ||
request_id=au.request_id, | ||
uid=au.u.id, | ||
code=const.CodeEnum.INVALID_SETTING, | ||
language=au.language, | ||
log_msg=f"auth is None or empty", | ||
) | ||
llm_service = LLM_DEFAULT_SERVICES[service] | ||
resp, code = await llm_service.complete( | ||
messages=[{"role": "user", "content": "hi"}], | ||
model=model, | ||
req_id=au.request_id, | ||
) | ||
print(resp) | ||
maybe_raise_json_exception(au=au, code=code) | ||
|
||
return schemas.RequestIdResponse( | ||
requestId=au.request_id, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,51 @@ | ||
from typing import Dict | ||
|
||
from .aliyun import AliyunService, AliyunModelEnum | ||
from .baidu import BaiduService, BaiduModelEnum | ||
from .base import BaseLLMService | ||
from .moonshot import MoonshotService, MoonshotModelEnum | ||
from .openai import OpenaiService, OpenaiModelEnum | ||
from .tencent import TencentService, TencentModelEnum | ||
from .xfyun import XfYunService, XfYunModelEnum | ||
|
||
LLM_SERVICES_CLASS = { | ||
AliyunService.name: { | ||
"service": AliyunService, | ||
"models": AliyunModelEnum, | ||
}, | ||
BaiduService.name: { | ||
"service": BaiduService, | ||
"models": BaiduModelEnum, | ||
}, | ||
MoonshotService.name: { | ||
"service": MoonshotService, | ||
"models": MoonshotModelEnum, | ||
}, | ||
OpenaiService.name: { | ||
"service": OpenaiService, | ||
"models": OpenaiModelEnum, | ||
}, | ||
TencentService.name: { | ||
"service": TencentService, | ||
"models": TencentModelEnum, | ||
}, | ||
XfYunService.name: { | ||
"service": XfYunService, | ||
"models": XfYunModelEnum, | ||
}, | ||
} | ||
|
||
TOP_P = 0.9 | ||
TEMPERATURE = 0.6 | ||
TIMEOUT = 60 | ||
|
||
LLM_DEFAULT_SERVICES: Dict[str, BaseLLMService] = { | ||
s.name: s(top_p=TOP_P, temperature=TEMPERATURE, timeout=TIMEOUT) for s in [ | ||
TencentService, | ||
AliyunService, | ||
OpenaiService, | ||
MoonshotService, | ||
XfYunService, | ||
BaiduService, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.