File size: 1,979 Bytes
b4f2238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""API endpoints."""

from __future__ import annotations

"""
LLM API 共享实现
"""

from collections.abc import Callable
from typing import Any, cast


def chat_with_context(
    message: str,
    session_id: str | None = None,
    user_id: str | None = None,
    system_prompt: str | None = None,
    conversation_service_factory: Callable[..., Any] | None = None,
) -> dict[str, str]:
    conversation_service = conversation_service_factory(session_id=session_id, user_id=user_id)  # type: ignore[misc]
    response = conversation_service.chat_with_context(
        user_message=message,
        system_prompt=system_prompt,
    )
    return {"response": response, "session_id": conversation_service.session_id}


async def achat_with_context(
    message: str,
    session_id: str | None = None,
    user_id: str | None = None,
    system_prompt: str | None = None,
    conversation_service_factory: Callable[..., Any] | None = None,
) -> dict[str, str]:
    from asgiref.sync import sync_to_async

    def _run() -> Any:
        conversation_service = conversation_service_factory(session_id=session_id, user_id=user_id)  # type: ignore[misc]
        response = conversation_service.chat_with_context(
            user_message=message,
            system_prompt=system_prompt,
        )
        return {"response": response, "session_id": conversation_service.session_id}

    return cast(dict[str, str], await sync_to_async(_run, thread_sensitive=True)())


def _get_conversation_history_service() -> Any:
    from apps.core.services.conversation_history_service import ConversationHistoryService

    return ConversationHistoryService()


def get_conversation_history(session_id: str, user_id: str | None = None, limit: int = 50) -> dict[str, Any]:
    service = _get_conversation_history_service()
    messages = service.get_conversation_history_messages(session_id=session_id, user_id=user_id, limit=limit)
    return {"session_id": session_id, "messages": messages}