mirror of
https://github.com/zylon-ai/private-gpt.git
synced 2025-12-22 20:12:55 +01:00
refactor: split method in two method (summary)
This commit is contained in:
parent
292d6e895a
commit
d3a9d746b5
2 changed files with 54 additions and 15 deletions
|
|
@ -59,23 +59,28 @@ def summarize(
|
||||||
"""
|
"""
|
||||||
service: SummarizeService = request.state.injector.get(SummarizeService)
|
service: SummarizeService = request.state.injector.get(SummarizeService)
|
||||||
|
|
||||||
|
if body.stream:
|
||||||
|
completion_gen = service.stream_summarize(
|
||||||
|
text=body.text,
|
||||||
|
instructions=body.instructions,
|
||||||
|
use_context=body.use_context,
|
||||||
|
context_filter=body.context_filter,
|
||||||
|
prompt=body.prompt,
|
||||||
|
)
|
||||||
|
return StreamingResponse(
|
||||||
|
to_openai_sse_stream(
|
||||||
|
response_generator=completion_gen,
|
||||||
|
),
|
||||||
|
media_type="text/event-stream",
|
||||||
|
)
|
||||||
|
else:
|
||||||
completion = service.summarize(
|
completion = service.summarize(
|
||||||
text=body.text,
|
text=body.text,
|
||||||
instructions=body.instructions,
|
instructions=body.instructions,
|
||||||
use_context=body.use_context,
|
use_context=body.use_context,
|
||||||
context_filter=body.context_filter,
|
context_filter=body.context_filter,
|
||||||
prompt=body.prompt,
|
prompt=body.prompt,
|
||||||
stream=body.stream,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(completion, str):
|
|
||||||
return SummarizeResponse(
|
return SummarizeResponse(
|
||||||
summary=completion,
|
summary=completion,
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
return StreamingResponse(
|
|
||||||
to_openai_sse_stream(
|
|
||||||
response_generator=completion,
|
|
||||||
),
|
|
||||||
media_type="text/event-stream",
|
|
||||||
)
|
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ class SummarizeService:
|
||||||
if doc_id in context_filter.docs_ids
|
if doc_id in context_filter.docs_ids
|
||||||
]
|
]
|
||||||
|
|
||||||
def summarize(
|
def _summarize(
|
||||||
self,
|
self,
|
||||||
use_context: bool = False,
|
use_context: bool = False,
|
||||||
stream: bool = False,
|
stream: bool = False,
|
||||||
|
|
@ -133,3 +133,37 @@ class SummarizeService:
|
||||||
return response.response_gen
|
return response.response_gen
|
||||||
else:
|
else:
|
||||||
raise TypeError(f"The result is not of a supported type: {type(response)}")
|
raise TypeError(f"The result is not of a supported type: {type(response)}")
|
||||||
|
|
||||||
|
def summarize(
|
||||||
|
self,
|
||||||
|
use_context: bool = False,
|
||||||
|
text: str | None = None,
|
||||||
|
instructions: str | None = None,
|
||||||
|
context_filter: ContextFilter | None = None,
|
||||||
|
prompt: str | None = None,
|
||||||
|
) -> str:
|
||||||
|
return self._summarize(
|
||||||
|
use_context=use_context,
|
||||||
|
stream=False,
|
||||||
|
text=text,
|
||||||
|
instructions=instructions,
|
||||||
|
context_filter=context_filter,
|
||||||
|
prompt=prompt,
|
||||||
|
) # type: ignore
|
||||||
|
|
||||||
|
def stream_summarize(
|
||||||
|
self,
|
||||||
|
use_context: bool = False,
|
||||||
|
text: str | None = None,
|
||||||
|
instructions: str | None = None,
|
||||||
|
context_filter: ContextFilter | None = None,
|
||||||
|
prompt: str | None = None,
|
||||||
|
) -> TokenGen:
|
||||||
|
return self._summarize(
|
||||||
|
use_context=use_context,
|
||||||
|
stream=True,
|
||||||
|
text=text,
|
||||||
|
instructions=instructions,
|
||||||
|
context_filter=context_filter,
|
||||||
|
prompt=prompt,
|
||||||
|
) # type: ignore
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue