mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-16 00:14:31 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
71 lines
2.6 KiB
Python
71 lines
2.6 KiB
Python
from typing import Any, Dict, Generator
|
|
|
|
from docsgpt.llm.handlers.base import LLMHandler, LLMResponse, ToolCall
|
|
from docsgpt.llm.openai import OpenAILLM
|
|
|
|
|
|
class OpenAILLMHandler(LLMHandler):
|
|
"""Handler for OpenAI API."""
|
|
|
|
def parse_response(self, response: Any) -> LLMResponse:
|
|
"""Parse OpenAI response into standardized format."""
|
|
if isinstance(response, str):
|
|
return LLMResponse(
|
|
content=response,
|
|
tool_calls=[],
|
|
finish_reason="stop",
|
|
raw_response=response,
|
|
)
|
|
|
|
message = getattr(response, "message", None) or getattr(response, "delta", None)
|
|
|
|
tool_calls = []
|
|
if hasattr(message, "tool_calls"):
|
|
tool_calls = [
|
|
ToolCall(
|
|
id=getattr(tc, "id", ""),
|
|
name=getattr(tc.function, "name", ""),
|
|
arguments=getattr(tc.function, "arguments", ""),
|
|
index=getattr(tc, "index", None),
|
|
)
|
|
for tc in message.tool_calls or []
|
|
]
|
|
# Reasoning lives on the message object for non-streaming and
|
|
# on the delta for streaming. DeepSeek thinking mode requires
|
|
# this to be echoed back on the next turn.
|
|
reasoning_content = OpenAILLM._extract_reasoning_text(message)
|
|
return LLMResponse(
|
|
content=getattr(message, "content", ""),
|
|
tool_calls=tool_calls,
|
|
finish_reason=getattr(response, "finish_reason", ""),
|
|
raw_response=response,
|
|
reasoning_content=reasoning_content,
|
|
)
|
|
|
|
def create_tool_message(self, tool_call: ToolCall, result: Any) -> Dict:
|
|
"""Create a tool result message in the standard internal format."""
|
|
import json as _json
|
|
|
|
from docsgpt.storage.db.serialization import PGNativeJSONEncoder
|
|
|
|
# PostgresTool results commonly include PG-native types
|
|
# (datetime / UUID / Decimal / bytea) when SELECT touches
|
|
# timestamptz / numeric / uuid / bytea columns. The shared
|
|
# encoder handles all five — bytes get base64 (lossless) instead
|
|
# of the ``str(b'...')`` repr that ``default=str`` would emit.
|
|
content = (
|
|
_json.dumps(result, cls=PGNativeJSONEncoder)
|
|
if not isinstance(result, str)
|
|
else result
|
|
)
|
|
return {
|
|
"role": "tool",
|
|
"tool_call_id": tool_call.id,
|
|
"content": content,
|
|
}
|
|
|
|
def _iterate_stream(self, response: Any) -> Generator:
|
|
"""Iterate through OpenAI streaming response."""
|
|
for chunk in response:
|
|
yield chunk
|