Spaces:
Sleeping
Sleeping
Update private_gpt/server/completions/completions_router.py
Browse files
private_gpt/server/completions/completions_router.py
CHANGED
@@ -36,92 +36,51 @@ class CompletionsBody(BaseModel):
|
|
36 |
}
|
37 |
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
51 |
-
|
52 |
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
#
|
75 |
-
|
76 |
-
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
|
87 |
-
@chat_router.post(
|
88 |
-
"/chat/completions",
|
89 |
-
response_model=None,
|
90 |
-
responses={200: {"model": OpenAICompletion}},
|
91 |
-
tags=["Contextual Completions"],
|
92 |
-
)
|
93 |
-
def chat_completion(
|
94 |
-
request: Request, body: ChatBody
|
95 |
-
) -> OpenAICompletion | StreamingResponse:
|
96 |
-
"""Given a list of messages comprising a conversation, return a response."""
|
97 |
-
try:
|
98 |
-
service = request.state.injector.get(ChatService)
|
99 |
-
all_messages = [
|
100 |
-
ChatMessage(content=m.content, role=MessageRole(m.role)) for m in body.messages
|
101 |
-
]
|
102 |
-
if body.stream:
|
103 |
-
completion_gen = service.stream_chat(
|
104 |
-
messages=all_messages,
|
105 |
-
use_context=body.use_context,
|
106 |
-
context_filter=body.context_filter,
|
107 |
-
)
|
108 |
-
return StreamingResponse(
|
109 |
-
to_openai_sse_stream(
|
110 |
-
completion_gen.response,
|
111 |
-
completion_gen.sources if body.include_sources else None,
|
112 |
-
),
|
113 |
-
media_type="text/event-stream",
|
114 |
-
)
|
115 |
-
else:
|
116 |
-
completion = service.chat(
|
117 |
-
messages=all_messages,
|
118 |
-
use_context=body.use_context,
|
119 |
-
context_filter=body.context_filter,
|
120 |
-
)
|
121 |
-
return to_openai_response(
|
122 |
-
completion.response, completion.sources if body.include_sources else None
|
123 |
-
)
|
124 |
-
except Exception as e:
|
125 |
-
# Log the exception details for debugging
|
126 |
-
print(f"Error processing chat completion: {e}")
|
127 |
-
return {"error": {"message": "Internal server error"}}
|
|
|
36 |
}
|
37 |
|
38 |
|
39 |
+
@completions_router.post(
|
40 |
+
"/completions",
|
41 |
+
response_model=None,
|
42 |
+
summary="Completion",
|
43 |
+
responses={200: {"model": OpenAICompletion}},
|
44 |
+
tags=["Contextual Completions"],
|
45 |
+
)
|
46 |
+
def prompt_completion(
|
47 |
+
request: Request, body: CompletionsBody
|
48 |
+
) -> OpenAICompletion | StreamingResponse:
|
49 |
+
"""We recommend most users use our Chat completions API.
|
50 |
|
51 |
+
Given a prompt, the model will return one predicted completion.
|
52 |
|
53 |
+
Optionally include a `system_prompt` to influence the way the LLM answers.
|
54 |
|
55 |
+
If `use_context`
|
56 |
+
is set to `true`, the model will use context coming from the ingested documents
|
57 |
+
to create the response. The documents being used can be filtered using the
|
58 |
+
`context_filter` and passing the document IDs to be used. Ingested documents IDs
|
59 |
+
can be found using `/ingest/list` endpoint. If you want all ingested documents to
|
60 |
+
be used, remove `context_filter` altogether.
|
61 |
|
62 |
+
When using `'include_sources': true`, the API will return the source Chunks used
|
63 |
+
to create the response, which come from the context provided.
|
64 |
|
65 |
+
When using `'stream': true`, the API will return data chunks following [OpenAI's
|
66 |
+
streaming model](https://platform.openai.com/docs/api-reference/chat/streaming):
|
67 |
+
```
|
68 |
+
{"id":"12345","object":"completion.chunk","created":1694268190,
|
69 |
+
"model":"private-gpt","choices":[{"index":0,"delta":{"content":"Hello"},
|
70 |
+
"finish_reason":null}]}
|
71 |
+
```
|
72 |
+
"""
|
73 |
+
messages = [OpenAIMessage(content=body.prompt, role="user")]
|
74 |
+
# If system prompt is passed, create a fake message with the system prompt.
|
75 |
+
if body.system_prompt:
|
76 |
+
messages.insert(0, OpenAIMessage(content=body.system_prompt, role="system"))
|
77 |
|
78 |
+
chat_body = ChatBody(
|
79 |
+
messages=messages,
|
80 |
+
use_context=body.use_context,
|
81 |
+
stream=body.stream,
|
82 |
+
include_sources=body.include_sources,
|
83 |
+
context_filter=body.context_filter,
|
84 |
+
)
|
85 |
+
return chat_completion(request, chat_body)
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|