responses.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from typing import Any, Optional
  2. from pydantic import BaseModel, Field
  3. from shared.abstractions import ChunkSearchResult, GraphSearchResult, Message
  4. from shared.abstractions.llm import LLMChatCompletion
  5. from shared.api.models.base import R2RResults
  6. class CombinedSearchResponse(BaseModel):
  7. chunk_search_results: list[ChunkSearchResult] = Field(
  8. ...,
  9. description="List of vector search results",
  10. )
  11. graph_search_results: Optional[list[GraphSearchResult]] = Field(
  12. None,
  13. description="Knowledge graph search results, if applicable",
  14. )
  15. class Config:
  16. json_schema_extra = {
  17. "example": {
  18. "chunk_search_results": [
  19. ChunkSearchResult.Config.json_schema_extra,
  20. ],
  21. "graph_search_results": [
  22. GraphSearchResult.Config.json_schema_extra,
  23. ],
  24. }
  25. }
  26. class RAGResponse(BaseModel):
  27. completion: Any = Field(
  28. ...,
  29. description="The generated completion from the RAG process",
  30. )
  31. search_results: CombinedSearchResponse = Field(
  32. ...,
  33. description="The search results used for the RAG process",
  34. )
  35. class Config:
  36. json_schema_extra = {
  37. "example": {
  38. "completion": {
  39. "id": "chatcmpl-example123",
  40. "choices": [
  41. {
  42. "finish_reason": "stop",
  43. "index": 0,
  44. "logprobs": None,
  45. "message": {
  46. "content": "Paris is the capital of France.",
  47. "role": "assistant",
  48. },
  49. }
  50. ],
  51. },
  52. "search_results": {
  53. "chunk_search_results": [
  54. ChunkSearchResult.Config.json_schema_extra,
  55. ],
  56. "graph_search_results": [
  57. GraphSearchResult.Config.json_schema_extra,
  58. ],
  59. },
  60. }
  61. }
  62. class AgentResponse(BaseModel):
  63. messages: list[Message] = Field(..., description="Agent response messages")
  64. conversation_id: str = Field(
  65. ..., description="The conversation ID for the RAG agent response"
  66. )
  67. class Config:
  68. json_schema_extra = {
  69. "example": {
  70. "messages": [
  71. {
  72. "role": "assistant",
  73. "content": None,
  74. "name": None,
  75. "function_call": {
  76. "name": "search",
  77. "arguments": '{"query":"Aristotle biography"}',
  78. },
  79. "tool_calls": None,
  80. },
  81. {
  82. "role": "function",
  83. "content": "1. Aristotle[A] (Greek: Ἀριστοτέλης Aristotélēs, pronounced [aristotélɛːs]; 384–322 BC) was an Ancient Greek philosopher and polymath. His writings cover a broad range of subjects spanning the natural sciences, philosophy, linguistics, economics, politics, psychology, and the arts. As the founder of the Peripatetic school of philosophy in the Lyceum in Athens, he began the wider Aristotelian tradition that followed, which set the groundwork for the development of modern science.\n2. Aristotle[A] (Greek: Ἀριστοτέλης Aristotélēs, pronounced [aristotélɛːs]; 384–322 BC) was an Ancient Greek philosopher and polymath. His writings cover a broad range of subjects spanning the natural sciences, philosophy, linguistics, economics, politics, psychology, and the arts. As the founder of the Peripatetic school of philosophy in the Lyceum in Athens, he began the wider Aristotelian tradition that followed, which set the groundwork for the development of modern science.\n3. Aristotle was born in 384 BC[C] in Stagira, Chalcidice,[2] about 55 km (34 miles) east of modern-day Thessaloniki.[3][4] His father, Nicomachus, was the personal physician to King Amyntas of Macedon. While he was young, Aristotle learned about biology and medical information, which was taught by his father.[5] Both of Aristotle's parents died when he was about thirteen, and Proxenus of Atarneus became his guardian.[6] Although little information about Aristotle's childhood has survived, he probably spent\n4. Aristotle was born in 384 BC[C] in Stagira, Chalcidice,[2] about 55 km (34 miles) east of modern-day Thessaloniki.[3][4] His father, Nicomachus, was the personal physician to King Amyntas of Macedon. While he was young, Aristotle learned about biology and medical information, which was taught by his father.[5] Both of Aristotle's parents died when he was about thirteen, and Proxenus of Atarneus became his guardian.[6] Although little information about Aristotle's childhood has survived, he probably spent\n5. Life\nIn general, the details of Aristotle's life are not well-established. The biographies written in ancient times are often speculative and historians only agree on a few salient points.[B]\n",
  84. "name": "search",
  85. "function_call": None,
  86. "tool_calls": None,
  87. },
  88. {
  89. "role": "assistant",
  90. "content": "Aristotle (384–322 BC) was an Ancient Greek philosopher and polymath whose contributions have had a profound impact on various fields of knowledge. Here are some key points about his life and work:\n\n1. **Early Life**: Aristotle was born in 384 BC in Stagira, Chalcidice, which is near modern-day Thessaloniki, Greece. His father, Nicomachus, was the personal physician to King Amyntas of Macedon, which exposed Aristotle to medical and biological knowledge from a young age [C].\n\n2. **Education and Career**: After the death of his parents, Aristotle was sent to Athens to study at Plato's Academy, where he remained for about 20 years. After Plato's death, Aristotle left Athens and eventually became the tutor of Alexander the Great [C].\n\n3. **Philosophical Contributions**: Aristotle founded the Lyceum in Athens, where he established the Peripatetic school of philosophy. His works cover a wide range of subjects, including metaphysics, ethics, politics, logic, biology, and aesthetics. His writings laid the groundwork for many modern scientific and philosophical inquiries [A].\n\n4. **Legacy**: Aristotle's influence extends beyond philosophy to the natural sciences, linguistics, economics, and psychology. His method of systematic observation and analysis has been foundational to the development of modern science [A].\n\nAristotle's comprehensive approach to knowledge and his systematic methodology have earned him a lasting legacy as one of the greatest philosophers of all time.\n\nSources:\n- [A] Aristotle's broad range of writings and influence on modern science.\n- [C] Details about Aristotle's early life and education.",
  91. "name": None,
  92. "function_call": None,
  93. "tool_calls": None,
  94. },
  95. ],
  96. "conversation_id": "a32b4c5d-6e7f-8a9b-0c1d-2e3f4a5b6c7d",
  97. }
  98. }
  99. class DocumentSearchResult(BaseModel):
  100. document_id: str = Field(
  101. ...,
  102. description="The document ID",
  103. )
  104. metadata: Optional[dict] = Field(
  105. None,
  106. description="The metadata of the document",
  107. )
  108. score: float = Field(
  109. ...,
  110. description="The score of the document",
  111. )
  112. WrappedCompletionResponse = R2RResults[LLMChatCompletion]
  113. # Create wrapped versions of the responses
  114. WrappedVectorSearchResponse = R2RResults[list[ChunkSearchResult]]
  115. WrappedSearchResponse = R2RResults[CombinedSearchResponse]
  116. WrappedDocumentSearchResponse = R2RResults[list[DocumentSearchResult]]
  117. WrappedRAGResponse = R2RResults[RAGResponse]
  118. WrappedAgentResponse = R2RResults[AgentResponse]