|
95 | 95 | "import os\n", |
96 | 96 | "from os import linesep\n", |
97 | 97 | "from pathlib import Path\n", |
98 | | - "from typing import Callable, Optional, Sequence\n", |
| 98 | + "from typing import Callable, Sequence\n", |
99 | 99 | "\n", |
100 | 100 | "from llama_index.observability.otel import LlamaIndexOpenTelemetry\n", |
101 | 101 | "from opentelemetry.sdk.trace import ReadableSpan\n", |
|
113 | 113 | " def __init__(\n", |
114 | 114 | " self,\n", |
115 | 115 | " service_name: str | None = None,\n", |
116 | | - " file_path: Optional[os.PathLike[str]] = None,\n", |
| 116 | + " file_path: os.PathLike[str] | None = None,\n", |
117 | 117 | " formatter: Callable[[ReadableSpan], str] = lambda span: json.dumps(\n", |
118 | 118 | " json.loads(span.to_json())\n", |
119 | 119 | " )\n", |
|
186 | 186 | }, |
187 | 187 | "outputs": [], |
188 | 188 | "source": [ |
189 | | - "from typing import Dict, List, Literal\n", |
| 189 | + "from typing import Literal\n", |
190 | 190 | "\n", |
191 | 191 | "from workflows.events import (\n", |
192 | 192 | " Event,\n", |
|
208 | 208 | "\n", |
209 | 209 | " sender_details: str\n", |
210 | 210 | " support_category: Literal[\"technical\", \"sales\", \"general_information\"]\n", |
211 | | - " support_questions: List[str]\n", |
| 211 | + " support_questions: list[str]\n", |
212 | 212 | " extra_information: str\n", |
213 | 213 | "\n", |
214 | 214 | "\n", |
215 | 215 | "class QuestionEvent(Event):\n", |
216 | 216 | " \"\"\"Event containing one or more questions to retrieve information from the company database.\"\"\"\n", |
217 | 217 | "\n", |
218 | | - " questions: List[str]\n", |
| 218 | + " questions: list[str]\n", |
219 | 219 | "\n", |
220 | 220 | "\n", |
221 | 221 | "class RetrievalEvent(Event):\n", |
222 | 222 | " \"\"\"Event that contains the retrieved information\"\"\"\n", |
223 | 223 | "\n", |
224 | | - " information: Dict[str, str]\n", |
| 224 | + " information: dict[str, str]\n", |
225 | 225 | "\n", |
226 | 226 | "\n", |
227 | 227 | "class CandidateEmailEvent(InputRequiredEvent):\n", |
|
338 | 338 | "class SenderDetails(BaseModel):\n", |
339 | 339 | " name: str = Field(description=\"Name of the sender seeking support\")\n", |
340 | 340 | " email_address: str = Field(description=\"Email address of the sender\")\n", |
341 | | - " company: Optional[str] = Field(\n", |
| 341 | + " company: str | None = Field(\n", |
342 | 342 | " descrption=\"Company for which the sender works\", default=None\n", |
343 | 343 | " )\n", |
344 | | - " role: Optional[str] = Field(descrption=\"Job role of the sender\", default=None)\n", |
| 344 | + " role: str | None = Field(descrption=\"Job role of the sender\", default=None)\n", |
345 | 345 | "\n", |
346 | 346 | "\n", |
347 | 347 | "class SupportEmailInformation(BaseModel):\n", |
348 | 348 | " sender_details: SenderDetails\n", |
349 | 349 | " support_category: Literal[\"technical\", \"sales\", \"general_information\"]\n", |
350 | | - " support_questions: List[str]\n", |
| 350 | + " support_questions: list[str]\n", |
351 | 351 | " extra_information: str" |
352 | 352 | ] |
353 | 353 | }, |
|
496 | 496 | }, |
497 | 497 | "outputs": [], |
498 | 498 | "source": [ |
499 | | - "files: List[dict] = []\n", |
| 499 | + "files: list[dict] = []\n", |
500 | 500 | "fls = [os.path.join(\"data\", f) for f in os.listdir(\"data/\")]\n", |
501 | 501 | "\n", |
502 | 502 | "file_ids = []\n", |
|
535 | 535 | "\n", |
536 | 536 | "\n", |
537 | 537 | "class SupportQuestions(BaseModel):\n", |
538 | | - " questions: List[str] = Field(\n", |
| 538 | + " questions: list[str] = Field(\n", |
539 | 539 | " description=\"Support questions to ask within the company database to retrieve the most information about the users' requests\"\n", |
540 | 540 | " )\n", |
541 | 541 | "\n", |
|
604 | 604 | "outputs": [], |
605 | 605 | "source": [ |
606 | 606 | "import uuid\n", |
607 | | - "from typing import Annotated, Union\n", |
| 607 | + "from typing import Annotated\n", |
608 | 608 | "\n", |
609 | 609 | "from llama_cloud_services.extract import SourceText\n", |
610 | 610 | "from llama_index.core.llms import ChatMessage\n", |
|
620 | 620 | " event: EmailEvent,\n", |
621 | 621 | " ctx: Context[WorkflowState],\n", |
622 | 622 | " extraction_agent: Annotated[ExtractionAgent, Resource(get_extract_agent)],\n", |
623 | | - " ) -> Union[InformationExtractionEvent, SendEmailEvent]:\n", |
| 623 | + " ) -> InformationExtractionEvent | SendEmailEvent:\n", |
624 | 624 | " extracted_content = await extraction_agent.aextract(\n", |
625 | 625 | " files=SourceText(\n", |
626 | 626 | " text_content=text, filename=f\"support_email_{str(uuid.uuid4())}.txt\"\n", |
|
656 | 656 | " event: InformationExtractionEvent,\n", |
657 | 657 | " ctx: Context[WorkflowState],\n", |
658 | 658 | " llm: Annotated[StructuredLLM, Resource(get_llm)],\n", |
659 | | - " ) -> Union[QuestionEvent, SendEmailEvent]:\n", |
| 659 | + " ) -> QuestionEvent | SendEmailEvent:\n", |
660 | 660 | " response = await llm.achat(\n", |
661 | 661 | " messages=[\n", |
662 | 662 | " ChatMessage(\n", |
|
685 | 685 | " ctx: Context[WorkflowState],\n", |
686 | 686 | " company_database: Annotated[BaseQueryEngine, Resource(get_company_database)],\n", |
687 | 687 | " ) -> RetrievalEvent:\n", |
688 | | - " q_and_a: Dict[str, str] = {}\n", |
| 688 | + " q_and_a: dict[str, str] = {}\n", |
689 | 689 | " for question in event.questions:\n", |
690 | 690 | " response = await company_database.aquery(question)\n", |
691 | 691 | " q_and_a.update({question: response.response or \"\"})\n", |
|
719 | 719 | " @step\n", |
720 | 720 | " async def send_email_or_restart(\n", |
721 | 721 | " self, event: HumanFeedbackEvent, ctx: Context[WorkflowState]\n", |
722 | | - " ) -> Union[SendEmailEvent, EmailEvent]:\n", |
| 722 | + " ) -> SendEmailEvent | EmailEvent:\n", |
723 | 723 | " if event.approved:\n", |
724 | 724 | " state = await ctx.store.get_state()\n", |
725 | 725 | " return SendEmailEvent(\n", |
|
1411 | 1411 | "source": [ |
1412 | 1412 | "import json\n", |
1413 | 1413 | "import time\n", |
1414 | | - "from typing import Annotated, Union\n", |
| 1414 | + "from typing import Annotated\n", |
1415 | 1415 | "\n", |
1416 | 1416 | "from workflows import Context, Workflow, step\n", |
1417 | 1417 | "from workflows.resource import Resource\n", |
|
1425 | 1425 | " event: EmailEvent,\n", |
1426 | 1426 | " ctx: Context[WorkflowState],\n", |
1427 | 1427 | " extraction_agent: Annotated[ExtractionAgent, Resource(get_extract_agent)],\n", |
1428 | | - " ) -> Union[InformationExtractionEvent, SendEmailEvent]:\n", |
| 1428 | + " ) -> InformationExtractionEvent | SendEmailEvent:\n", |
1429 | 1429 | " async with ctx.store.edit_state() as state:\n", |
1430 | 1430 | " state.email_text = event.email_content\n", |
1431 | 1431 | "\n", |
|
1468 | 1468 | " event: InformationExtractionEvent,\n", |
1469 | 1469 | " ctx: Context[WorkflowState],\n", |
1470 | 1470 | " llm: Annotated[StructuredLLM, Resource(get_llm)],\n", |
1471 | | - " ) -> Union[QuestionEvent, SendEmailEvent]:\n", |
| 1471 | + " ) -> QuestionEvent | SendEmailEvent:\n", |
1472 | 1472 | " response = await llm.achat(\n", |
1473 | 1473 | " messages=[\n", |
1474 | 1474 | " ChatMessage(\n", |
|
1503 | 1503 | " ctx: Context[WorkflowState],\n", |
1504 | 1504 | " company_database: Annotated[BaseQueryEngine, Resource(get_company_database)],\n", |
1505 | 1505 | " ) -> RetrievalEvent:\n", |
1506 | | - " q_and_a: Dict[str, str] = {}\n", |
| 1506 | + " q_and_a: dict[str, str] = {}\n", |
1507 | 1507 | " st = time.time()\n", |
1508 | 1508 | " for question in event.questions:\n", |
1509 | 1509 | " response = await company_database.aquery(question)\n", |
|
1545 | 1545 | " @step\n", |
1546 | 1546 | " async def send_email_or_restart(\n", |
1547 | 1547 | " self, event: HumanFeedbackEvent, ctx: Context[WorkflowState]\n", |
1548 | | - " ) -> Union[SendEmailEvent, EmailEvent]:\n", |
| 1548 | + " ) -> SendEmailEvent | EmailEvent:\n", |
1549 | 1549 | " if event.approved:\n", |
1550 | 1550 | " state = await ctx.store.get_state()\n", |
1551 | 1551 | " dispatcher.event(event=WorkflowDoneEvent(error=False))\n", |
|
0 commit comments