Skip to content

Commit

Permalink
feat: call xunfei spark with stream, and fix the temperature bug (eos…
Browse files Browse the repository at this point in the history
…phoros-ai#2121)

Co-authored-by: aries_ckt <[email protected]>
  • Loading branch information
hiyizi and Aries-ckt authored Nov 19, 2024
1 parent 4efe643 commit 3ccfa94
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 255 deletions.
6 changes: 2 additions & 4 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,8 @@ TONGYI_PROXY_API_KEY={your-tongyi-sk}
#BAICHUAN_PROXY_API_SECRET={your-baichuan-sct}

# Xunfei Spark
#XUNFEI_SPARK_API_VERSION={version}
#XUNFEI_SPARK_APPID={your_app_id}
#XUNFEI_SPARK_API_KEY={your_api_key}
#XUNFEI_SPARK_API_SECRET={your_api_secret}
#XUNFEI_SPARK_API_PASSWORD={your_api_password}
#XUNFEI_SPARK_API_MODEL={version}

## Yi Proxyllm, https://platform.lingyiwanwu.com/docs
#YI_MODEL_VERSION=yi-34b-chat-0205
Expand Down
82 changes: 41 additions & 41 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
# Please run command `pre-commit install` to install pre-commit hook
repos:
- repo: local
hooks:
- id: python-fmt
name: Python Format
entry: make fmt-check
language: system
exclude: '^dbgpt/app/static/|^web/'
types: [python]
stages: [commit]
pass_filenames: false
args: []
- id: python-test
name: Python Unit Test
entry: make test
language: system
exclude: '^dbgpt/app/static/|^web/'
types: [python]
stages: [commit]
pass_filenames: false
args: []
- id: python-test-doc
name: Python Doc Test
entry: make test-doc
language: system
exclude: '^dbgpt/app/static/|^web/'
types: [python]
stages: [commit]
pass_filenames: false
args: []
- id: python-lint-mypy
name: Python Lint mypy
entry: make mypy
language: system
exclude: '^dbgpt/app/static/|^web/'
types: [python]
stages: [commit]
pass_filenames: false
args: []

# Please run command `pre-commit install` to install pre-commit hook
repos:
- repo: local
hooks:
- id: python-fmt
name: Python Format
entry: make fmt-check
language: system
exclude: '^dbgpt/app/static/|^web/'
types: [python]
stages: [commit]
pass_filenames: false
args: []
- id: python-test
name: Python Unit Test
entry: make test
language: system
exclude: '^dbgpt/app/static/|^web/'
types: [python]
stages: [commit]
pass_filenames: false
args: []
- id: python-test-doc
name: Python Doc Test
entry: make test-doc
language: system
exclude: '^dbgpt/app/static/|^web/'
types: [python]
stages: [commit]
pass_filenames: false
args: []
- id: python-lint-mypy
name: Python Lint mypy
entry: make mypy
language: system
exclude: '^dbgpt/app/static/|^web/'
types: [python]
stages: [commit]
pass_filenames: false
args: []

18 changes: 7 additions & 11 deletions dbgpt/_private/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,13 @@ def __init__(self) -> None:
)

# xunfei spark
self.spark_api_version = os.getenv("XUNFEI_SPARK_API_VERSION")
self.spark_proxy_api_key = os.getenv("XUNFEI_SPARK_API_KEY")
self.spark_proxy_api_secret = os.getenv("XUNFEI_SPARK_API_SECRET")
self.spark_proxy_api_appid = os.getenv("XUNFEI_SPARK_APPID")
if self.spark_proxy_api_key and self.spark_proxy_api_secret:
os.environ["spark_proxyllm_proxy_api_key"] = self.spark_proxy_api_key
os.environ["spark_proxyllm_proxy_api_secret"] = self.spark_proxy_api_secret
os.environ["spark_proxyllm_proxyllm_backend"] = self.spark_api_version or ""
os.environ["spark_proxyllm_proxy_api_app_id"] = (
self.spark_proxy_api_appid or ""
)
self.spark_proxy_api_password = os.getenv("XUNFEI_SPARK_API_PASSWORD")
self.spark_proxy_api_model = os.getenv("XUNFEI_SPARK_API_MODEL")
if self.spark_proxy_api_model and self.spark_proxy_api_password:
os.environ[
"spark_proxyllm_proxy_api_password"
] = self.spark_proxy_api_password
os.environ["spark_proxyllm_proxy_api_model"] = self.spark_proxy_api_model

# baichuan proxy
self.bc_proxy_api_key = os.getenv("BAICHUAN_PROXY_API_KEY")
Expand Down
1 change: 1 addition & 0 deletions dbgpt/agent/core/plan/awel/agent_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ async def get_agent(
prompt_template = None
if self.awel_agent.agent_prompt:
from dbgpt.serve.prompt.api.endpoints import get_service

prompt_service = get_service()
prompt_template = prompt_service.get_template(
self.awel_agent.agent_prompt.code
Expand Down
2 changes: 2 additions & 0 deletions dbgpt/agent/core/plan/awel/agent_operator_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ResourceCategory,
register_resource,
)

from ....resource.base import AgentResource, ResourceType
from ....resource.manage import get_resource_manager
from ....util.llm.llm import LLMConfig, LLMStrategyType
Expand All @@ -20,6 +21,7 @@

def _agent_resource_prompt_values() -> List[OptionValue]:
from dbgpt.serve.prompt.api.endpoints import get_service

prompt_service = get_service()
prompts = prompt_service.get_target_prompt()
return [
Expand Down
132 changes: 67 additions & 65 deletions dbgpt/app/knowledge/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import os
import shutil
import tempfile
from typing import List
from pathlib import Path
from typing import List

from fastapi import APIRouter, Depends, File, Form, UploadFile, HTTPException
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile

from dbgpt._private.config import Config
from dbgpt.app.knowledge.request.request import (
Expand Down Expand Up @@ -333,70 +333,72 @@ def document_delete(space_name: str, query_request: DocumentQueryRequest):

@router.post("/knowledge/{space_name}/document/upload")
async def document_upload(
space_name: str,
doc_name: str = Form(...),
doc_type: str = Form(...),
doc_file: UploadFile = File(...),
space_name: str,
doc_name: str = Form(...),
doc_type: str = Form(...),
doc_file: UploadFile = File(...),
):
print(f"/document/upload params: {space_name}")
try:
if doc_file:
# Sanitize inputs to prevent path traversal
safe_space_name = os.path.basename(space_name)
safe_filename = os.path.basename(doc_file.filename)

# Create absolute paths and verify they are within allowed directory
upload_dir = os.path.abspath(os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, safe_space_name))
target_path = os.path.abspath(os.path.join(upload_dir, safe_filename))

if not os.path.abspath(KNOWLEDGE_UPLOAD_ROOT_PATH) in target_path:
raise HTTPException(status_code=400, detail="Invalid path detected")

if not os.path.exists(upload_dir):
os.makedirs(upload_dir)

# Create temp file
tmp_fd, tmp_path = tempfile.mkstemp(dir=upload_dir)

try:
with os.fdopen(tmp_fd, "wb") as tmp:
tmp.write(await doc_file.read())

shutil.move(tmp_path, target_path)

request = KnowledgeDocumentRequest()
request.doc_name = doc_name
request.doc_type = doc_type
request.content = target_path

space_res = knowledge_space_service.get_knowledge_space(
KnowledgeSpaceRequest(name=safe_space_name)
)
if len(space_res) == 0:
# create default space
if "default" != safe_space_name:
raise Exception(f"you have not create your knowledge space.")
knowledge_space_service.create_knowledge_space(
KnowledgeSpaceRequest(
name=safe_space_name,
desc="first db-gpt rag application",
owner="dbgpt",
)
)
return Result.succ(
knowledge_space_service.create_knowledge_document(
space=safe_space_name, request=request
)
)
except Exception as e:
# Clean up temp file if anything goes wrong
if os.path.exists(tmp_path):
os.unlink(tmp_path)
raise e

return Result.failed(code="E000X", msg=f"doc_file is None")
except Exception as e:
return Result.failed(code="E000X", msg=f"document add error {e}")
print(f"/document/upload params: {space_name}")
try:
if doc_file:
# Sanitize inputs to prevent path traversal
safe_space_name = os.path.basename(space_name)
safe_filename = os.path.basename(doc_file.filename)

# Create absolute paths and verify they are within allowed directory
upload_dir = os.path.abspath(
os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, safe_space_name)
)
target_path = os.path.abspath(os.path.join(upload_dir, safe_filename))

if not os.path.abspath(KNOWLEDGE_UPLOAD_ROOT_PATH) in target_path:
raise HTTPException(status_code=400, detail="Invalid path detected")

if not os.path.exists(upload_dir):
os.makedirs(upload_dir)

# Create temp file
tmp_fd, tmp_path = tempfile.mkstemp(dir=upload_dir)

try:
with os.fdopen(tmp_fd, "wb") as tmp:
tmp.write(await doc_file.read())

shutil.move(tmp_path, target_path)

request = KnowledgeDocumentRequest()
request.doc_name = doc_name
request.doc_type = doc_type
request.content = target_path

space_res = knowledge_space_service.get_knowledge_space(
KnowledgeSpaceRequest(name=safe_space_name)
)
if len(space_res) == 0:
# create default space
if "default" != safe_space_name:
raise Exception(f"you have not create your knowledge space.")
knowledge_space_service.create_knowledge_space(
KnowledgeSpaceRequest(
name=safe_space_name,
desc="first db-gpt rag application",
owner="dbgpt",
)
)
return Result.succ(
knowledge_space_service.create_knowledge_document(
space=safe_space_name, request=request
)
)
except Exception as e:
# Clean up temp file if anything goes wrong
if os.path.exists(tmp_path):
os.unlink(tmp_path)
raise e

return Result.failed(code="E000X", msg=f"doc_file is None")
except Exception as e:
return Result.failed(code="E000X", msg=f"document add error {e}")


@router.post("/knowledge/{space_name}/document/sync")
Expand Down
3 changes: 2 additions & 1 deletion dbgpt/app/scene/base_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ async def _build_model_request(self) -> ModelRequest:
)
node = AppChatComposerOperator(
model=self.llm_model,
temperature=float(self.prompt_template.temperature),
temperature=self._chat_param.get("temperature")
or float(self.prompt_template.temperature),
max_new_tokens=int(self.prompt_template.max_new_tokens),
prompt=self.prompt_template.prompt,
message_version=self._message_version,
Expand Down
Loading

0 comments on commit 3ccfa94

Please sign in to comment.