Ibraaheem commited on
Commit
02b6053
·
verified ·
1 Parent(s): f15c12f

Update private_gpt/server/chat/chat_router.py

Browse files
private_gpt/server/chat/chat_router.py CHANGED
@@ -49,6 +49,64 @@ class ChatBody(BaseModel):
49
  }
50
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  @chat_router.post(
53
  "/chat/completions",
54
  response_model=None,
@@ -58,51 +116,36 @@ class ChatBody(BaseModel):
58
  def chat_completion(
59
  request: Request, body: ChatBody
60
  ) -> OpenAICompletion | StreamingResponse:
61
- """Given a list of messages comprising a conversation, return a response.
62
-
63
- Optionally include an initial `role: system` message to influence the way
64
- the LLM answers.
65
-
66
- If `use_context` is set to `true`, the model will use context coming
67
- from the ingested documents to create the response. The documents being used can
68
- be filtered using the `context_filter` and passing the document IDs to be used.
69
- Ingested documents IDs can be found using `/ingest/list` endpoint. If you want
70
- all ingested documents to be used, remove `context_filter` altogether.
71
-
72
- When using `'include_sources': true`, the API will return the source Chunks used
73
- to create the response, which come from the context provided.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- When using `'stream': true`, the API will return data chunks following [OpenAI's
76
- streaming model](https://platform.openai.com/docs/api-reference/chat/streaming):
77
- ```
78
- {"id":"12345","object":"completion.chunk","created":1694268190,
79
- "model":"private-gpt","choices":[{"index":0,"delta":{"content":"Hello"},
80
- "finish_reason":null}]}
81
- ```
82
- """
83
- service = request.state.injector.get(ChatService)
84
- all_messages = [
85
- ChatMessage(content=m.content, role=MessageRole(m.role)) for m in body.messages
86
- ]
87
- if body.stream:
88
- completion_gen = service.stream_chat(
89
- messages=all_messages,
90
- use_context=body.use_context,
91
- context_filter=body.context_filter,
92
- )
93
- return StreamingResponse(
94
- to_openai_sse_stream(
95
- completion_gen.response,
96
- completion_gen.sources if body.include_sources else None,
97
- ),
98
- media_type="text/event-stream",
99
- )
100
- else:
101
- completion = service.chat(
102
- messages=all_messages,
103
- use_context=body.use_context,
104
- context_filter=body.context_filter,
105
- )
106
- return to_openai_response(
107
- completion.response, completion.sources if body.include_sources else None
108
- )
 
49
  }
50
 
51
 
52
+ # @chat_router.post(
53
+ # "/chat/completions",
54
+ # response_model=None,
55
+ # responses={200: {"model": OpenAICompletion}},
56
+ # tags=["Contextual Completions"],
57
+ # )
58
+ # def chat_completion(
59
+ # request: Request, body: ChatBody
60
+ # ) -> OpenAICompletion | StreamingResponse:
61
+ # """Given a list of messages comprising a conversation, return a response.
62
+
63
+ # Optionally include an initial `role: system` message to influence the way
64
+ # the LLM answers.
65
+
66
+ # If `use_context` is set to `true`, the model will use context coming
67
+ # from the ingested documents to create the response. The documents being used can
68
+ # be filtered using the `context_filter` and passing the document IDs to be used.
69
+ # Ingested documents IDs can be found using `/ingest/list` endpoint. If you want
70
+ # all ingested documents to be used, remove `context_filter` altogether.
71
+
72
+ # When using `'include_sources': true`, the API will return the source Chunks used
73
+ # to create the response, which come from the context provided.
74
+
75
+ # When using `'stream': true`, the API will return data chunks following [OpenAI's
76
+ # streaming model](https://platform.openai.com/docs/api-reference/chat/streaming):
77
+ # ```
78
+ # {"id":"12345","object":"completion.chunk","created":1694268190,
79
+ # "model":"private-gpt","choices":[{"index":0,"delta":{"content":"Hello"},
80
+ # "finish_reason":null}]}
81
+ # ```
82
+ # """
83
+ # service = request.state.injector.get(ChatService)
84
+ # all_messages = [
85
+ # ChatMessage(content=m.content, role=MessageRole(m.role)) for m in body.messages
86
+ # ]
87
+ # if body.stream:
88
+ # completion_gen = service.stream_chat(
89
+ # messages=all_messages,
90
+ # use_context=body.use_context,
91
+ # context_filter=body.context_filter,
92
+ # )
93
+ # return StreamingResponse(
94
+ # to_openai_sse_stream(
95
+ # completion_gen.response,
96
+ # completion_gen.sources if body.include_sources else None,
97
+ # ),
98
+ # media_type="text/event-stream",
99
+ # )
100
+ # else:
101
+ # completion = service.chat(
102
+ # messages=all_messages,
103
+ # use_context=body.use_context,
104
+ # context_filter=body.context_filter,
105
+ # )
106
+ # return to_openai_response(
107
+ # completion.response, completion.sources if body.include_sources else None
108
+ # )
109
+
110
  @chat_router.post(
111
  "/chat/completions",
112
  response_model=None,
 
116
  def chat_completion(
117
  request: Request, body: ChatBody
118
  ) -> OpenAICompletion | StreamingResponse:
119
+ """Given a list of messages comprising a conversation, return a response."""
120
+ try:
121
+ service = request.state.injector.get(ChatService)
122
+ all_messages = [
123
+ ChatMessage(content=m.content, role=MessageRole(m.role)) for m in body.messages
124
+ ]
125
+ if body.stream:
126
+ completion_gen = service.stream_chat(
127
+ messages=all_messages,
128
+ use_context=body.use_context,
129
+ context_filter=body.context_filter,
130
+ )
131
+ return StreamingResponse(
132
+ to_openai_sse_stream(
133
+ completion_gen.response,
134
+ completion_gen.sources if body.include_sources else None,
135
+ ),
136
+ media_type="text/event-stream",
137
+ )
138
+ else:
139
+ completion = service.chat(
140
+ messages=all_messages,
141
+ use_context=body.use_context,
142
+ context_filter=body.context_filter,
143
+ )
144
+ return to_openai_response(
145
+ completion.response, completion.sources if body.include_sources else None
146
+ )
147
+ except Exception as e:
148
+ # Log the exception details for debugging
149
+ print(f"Error processing chat completion: {e}")
150
+ return {"error": {"message": "Internal server error"}}
151