Spaces:
Running
Running
Update document_generator_v2.py
Browse files- document_generator_v2.py +12 -5
document_generator_v2.py
CHANGED
|
@@ -464,7 +464,7 @@ class MarkdownDocumentRequest(BaseModel):
|
|
| 464 |
json_document: Dict
|
| 465 |
query: str
|
| 466 |
template: bool = False
|
| 467 |
-
conversation_id: str =
|
| 468 |
|
| 469 |
MESSAGE_DELIMITER = b"\n---DELIMITER---\n"
|
| 470 |
|
|
@@ -472,12 +472,14 @@ def yield_message(message):
|
|
| 472 |
message_json = json.dumps(message, ensure_ascii=False).encode('utf-8')
|
| 473 |
return message_json + MESSAGE_DELIMITER
|
| 474 |
|
| 475 |
-
async def generate_document_stream(document_generator: DocumentGenerator, document_outline: Dict, query: str, template: bool = False):
|
| 476 |
document_generator.document_outline = document_outline
|
| 477 |
db_manager = DatabaseManager()
|
| 478 |
overall_objective = query
|
| 479 |
document_layout = json.dumps(document_generator.document_outline, indent=2)
|
| 480 |
-
|
|
|
|
|
|
|
| 481 |
SECTION_PROMPT_SYSTEM = DOCUMENT_SECTION_PROMPT_SYSTEM if not template else DOCUMENT_TEMPLATE_SECTION_PROMPT_SYSTEM
|
| 482 |
document_generator.content_messages = [
|
| 483 |
{
|
|
@@ -488,6 +490,8 @@ async def generate_document_stream(document_generator: DocumentGenerator, docume
|
|
| 488 |
)
|
| 489 |
}
|
| 490 |
]
|
|
|
|
|
|
|
| 491 |
|
| 492 |
for section in document_generator.document_outline["Document"].get("Sections", []):
|
| 493 |
section_title = section.get("Title", "")
|
|
@@ -540,7 +544,7 @@ async def generate_markdown_document_stream_endpoint(request: MarkdownDocumentRe
|
|
| 540 |
|
| 541 |
async def stream_generator():
|
| 542 |
try:
|
| 543 |
-
async for chunk in generate_document_stream(document_generator, request.json_document, request.query, request.template):
|
| 544 |
yield chunk
|
| 545 |
except Exception as e:
|
| 546 |
yield yield_message({
|
|
@@ -568,6 +572,10 @@ async def generate_document_outline_endpoint(
|
|
| 568 |
image_context = ""
|
| 569 |
if images:
|
| 570 |
image_context = await vision_tools.extract_images_info(images)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
|
| 572 |
json_document = document_generator.generate_document_outline(
|
| 573 |
query,
|
|
@@ -583,7 +591,6 @@ async def generate_document_outline_endpoint(
|
|
| 583 |
raise HTTPException(status_code=500, detail=str(e))
|
| 584 |
|
| 585 |
|
| 586 |
-
|
| 587 |
## OBSERVABILITY
|
| 588 |
from uuid import uuid4
|
| 589 |
import csv
|
|
|
|
| 464 |
json_document: Dict
|
| 465 |
query: str
|
| 466 |
template: bool = False
|
| 467 |
+
conversation_id: str = ""
|
| 468 |
|
| 469 |
MESSAGE_DELIMITER = b"\n---DELIMITER---\n"
|
| 470 |
|
|
|
|
| 472 |
message_json = json.dumps(message, ensure_ascii=False).encode('utf-8')
|
| 473 |
return message_json + MESSAGE_DELIMITER
|
| 474 |
|
| 475 |
+
async def generate_document_stream(document_generator: DocumentGenerator, document_outline: Dict, query: str, template: bool = False, conversation_id: str = ""):
|
| 476 |
document_generator.document_outline = document_outline
|
| 477 |
db_manager = DatabaseManager()
|
| 478 |
overall_objective = query
|
| 479 |
document_layout = json.dumps(document_generator.document_outline, indent=2)
|
| 480 |
+
cache_key = f"image_context_{conversation_id}"
|
| 481 |
+
image_context = await FastAPICache.get_backend().get(cache_key)
|
| 482 |
+
|
| 483 |
SECTION_PROMPT_SYSTEM = DOCUMENT_SECTION_PROMPT_SYSTEM if not template else DOCUMENT_TEMPLATE_SECTION_PROMPT_SYSTEM
|
| 484 |
document_generator.content_messages = [
|
| 485 |
{
|
|
|
|
| 490 |
)
|
| 491 |
}
|
| 492 |
]
|
| 493 |
+
if image_context:
|
| 494 |
+
document_generator.content_messages[0]["content"] += f"<attached_images>\n\n{image_context}\n\n</attached_images>"
|
| 495 |
|
| 496 |
for section in document_generator.document_outline["Document"].get("Sections", []):
|
| 497 |
section_title = section.get("Title", "")
|
|
|
|
| 544 |
|
| 545 |
async def stream_generator():
|
| 546 |
try:
|
| 547 |
+
async for chunk in generate_document_stream(document_generator, request.json_document, request.query, request.template, request.conversation_id):
|
| 548 |
yield chunk
|
| 549 |
except Exception as e:
|
| 550 |
yield yield_message({
|
|
|
|
| 572 |
image_context = ""
|
| 573 |
if images:
|
| 574 |
image_context = await vision_tools.extract_images_info(images)
|
| 575 |
+
|
| 576 |
+
# Store the image_context in the cache
|
| 577 |
+
cache_key = f"image_context_{conversation_id}"
|
| 578 |
+
await FastAPICache.get_backend().set(cache_key, image_context, expire=3600) # Cache for 1 hour
|
| 579 |
|
| 580 |
json_document = document_generator.generate_document_outline(
|
| 581 |
query,
|
|
|
|
| 591 |
raise HTTPException(status_code=500, detail=str(e))
|
| 592 |
|
| 593 |
|
|
|
|
| 594 |
## OBSERVABILITY
|
| 595 |
from uuid import uuid4
|
| 596 |
import csv
|