dv-pipecat-ai 0.0.75.dev870__py3-none-any.whl → 0.0.82.dev807__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dv-pipecat-ai might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dv-pipecat-ai
3
- Version: 0.0.75.dev870
3
+ Version: 0.0.82.dev807
4
4
  Summary: An open source framework for voice (and multimodal) assistants
5
5
  License-Expression: BSD-2-Clause
6
6
  Project-URL: Source, https://github.com/pipecat-ai/pipecat
@@ -1,4 +1,4 @@
1
- dv_pipecat_ai-0.0.75.dev870.dist-info/licenses/LICENSE,sha256=DWY2QGf2eMCFhuu2ChairtT6CB7BEFffNVhXWc4Od08,1301
1
+ dv_pipecat_ai-0.0.82.dev807.dist-info/licenses/LICENSE,sha256=DWY2QGf2eMCFhuu2ChairtT6CB7BEFffNVhXWc4Od08,1301
2
2
  pipecat/__init__.py,sha256=j0Xm6adxHhd7D06dIyyPV_GlBYLlBnTAERVvD_jAARQ,861
3
3
  pipecat/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  pipecat/adapters/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -46,7 +46,7 @@ pipecat/audio/vad/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU
46
46
  pipecat/audio/vad/silero.py,sha256=r9UL8aEe-QoRMNDGWLUlgUYew93-QFojE9sIqLO0VYE,7792
47
47
  pipecat/audio/vad/vad_analyzer.py,sha256=XkZLEe4z7Ja0lGoYZst1HNYqt5qOwG-vjsk_w8chiNA,7430
48
48
  pipecat/audio/vad/data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
49
- pipecat/audio/vad/data/silero_vad.onnx,sha256=JiOilT9v89LB5hdAxs23FoEzR5smff7xFKSjzFvdeI8,2327524
49
+ pipecat/audio/vad/data/silero_vad.onnx,sha256=WX0ws-wHZgjQWUd7sUz-_9-VG_XK43DTj2XTO7_oIAQ,2327524
50
50
  pipecat/clocks/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
51
51
  pipecat/clocks/base_clock.py,sha256=PuTmCtPKz5VG0VxhN5cyhbvduEBnfNPgA6GLAu1eSns,929
52
52
  pipecat/clocks/system_clock.py,sha256=ht6TdDAn0JVXEmhLdt5igcHMQOkKO4YHNuOjuKcxkUU,1315
@@ -126,6 +126,7 @@ pipecat/serializers/__init__.py,sha256=OV61GQX5ZVU7l7Dt7UTBdv2wUF7ZvtbCoXryo7nno
126
126
  pipecat/serializers/base_serializer.py,sha256=OyBUZccs2ZT9mfkBbq2tGsUJMvci6o-j90Cl1sicPaI,2030
127
127
  pipecat/serializers/convox.py,sha256=MXCLhV6GMnoP8bI6-EVrObhrftEyTGOmzVeIU5ywmPo,9536
128
128
  pipecat/serializers/exotel.py,sha256=LB4wYoXDjPmtkydrZ0G4H4u-SXpQw9KjyRzBZCYloEE,5907
129
+ pipecat/serializers/genesys.py,sha256=5g6_F-OIWSNmStgc6-bDT5mDQkCHHKxcOWSb-F4s2-A,3564
129
130
  pipecat/serializers/livekit.py,sha256=caVZlVJGV-SmEXE_H7i3DRo1RvC9FgGCVqi8IYGrpEo,2552
130
131
  pipecat/serializers/plivo.py,sha256=EXZZgwxQzhO61spRU98qveMskVnELuHCQg5piBO6tq0,9210
131
132
  pipecat/serializers/protobuf.py,sha256=h0UgVvIa3LXxtpbeQUq0tCGicGbDHxjiY6EdxXJO0_s,5162
@@ -200,6 +201,7 @@ pipecat/services/google/llm_openai.py,sha256=p_aQYpX1e_ffO63oo0cDyj8ZYWb2CO3N-Ii
200
201
  pipecat/services/google/llm_vertex.py,sha256=yqs8pqUCTgRj5wvQFHPJbGduoIaXjaqPym5x-lh5LhI,5032
201
202
  pipecat/services/google/rtvi.py,sha256=PZb1yVny5YG7_XmJRXPzs3iYapeQ4XHreFN1v6KwTGM,3014
202
203
  pipecat/services/google/stt.py,sha256=1vKZNEKZ-KLKp_7lA_VijznSqTwYRFYK1sDn2qteKtI,32814
204
+ pipecat/services/google/test-google-chirp.py,sha256=ji6ta7WDgKMu9yeKovuIVRlcMuk8S6XIyzIokHQY80E,1437
203
205
  pipecat/services/google/tts.py,sha256=S_JSPqzLABfuyHLRppNiDmq2g9OFcnJOrfysVg3OHbY,32038
204
206
  pipecat/services/grok/__init__.py,sha256=PyaTSnqwxd8jdF5aFTe3lWM-TBhfDyUu9ahRl6nPS-4,251
205
207
  pipecat/services/grok/llm.py,sha256=xsJWXqJApfQgEt6z_8U44qUCQJMcpgEdpOHN-u0tNAQ,7330
@@ -277,6 +279,8 @@ pipecat/services/together/__init__.py,sha256=hNMycJDDf3CLiL9WA9fwvMdYphyDWLv0Oab
277
279
  pipecat/services/together/llm.py,sha256=VSayO-U6g9Ld0xK9CXRQPUsd5gWJKtiA8qDAyXgsSkE,1958
278
280
  pipecat/services/ultravox/__init__.py,sha256=EoHCSXI2o0DFQslELgkhAGZtxDj63gZi-9ZEhXljaKE,259
279
281
  pipecat/services/ultravox/stt.py,sha256=uCQm_-LbycXdXRV6IE1a6Mymis6tyww7V8PnPzAQtx8,16586
282
+ pipecat/services/vistaar/__init__.py,sha256=UFfSWFN5rbzl6NN-E_OH_MFaSYodZWNlenAU0wk-rAI,110
283
+ pipecat/services/vistaar/llm.py,sha256=O-sznJDPivnhY_XUsr5xYcwkCqXpMv_zOuZ1rJBfn9Y,14631
280
284
  pipecat/services/whisper/__init__.py,sha256=smADmw0Fv98k7cGRuHTEcljKTO2WdZqLpJd0qsTCwH8,281
281
285
  pipecat/services/whisper/base_stt.py,sha256=VhslESPnYIeVbmnQTzmlZPV35TH49duxYTvJe0epNnE,7850
282
286
  pipecat/services/whisper/stt.py,sha256=9Qd56vWMzg3LtHikQnfgyMtl4odE6BCHDbpAn3HSWjw,17480
@@ -332,7 +336,7 @@ pipecat/utils/tracing/service_decorators.py,sha256=HwDCqLGijhYD3F8nxDuQmEw-YkRw0
332
336
  pipecat/utils/tracing/setup.py,sha256=7TEgPNpq6M8lww8OQvf0P9FzYc5A30xICGklVA-fua0,2892
333
337
  pipecat/utils/tracing/turn_context_provider.py,sha256=ikon3plFOx0XbMrH6DdeHttNpb-U0gzMZIm3bWLc9eI,2485
334
338
  pipecat/utils/tracing/turn_trace_observer.py,sha256=dma16SBJpYSOE58YDWy89QzHyQFc_9gQZszKeWixuwc,9725
335
- dv_pipecat_ai-0.0.75.dev870.dist-info/METADATA,sha256=TY5j2OnpfWw-gTs1G6j0XyoSIgd_j9cRmdS8sVGmPeM,32457
336
- dv_pipecat_ai-0.0.75.dev870.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
337
- dv_pipecat_ai-0.0.75.dev870.dist-info/top_level.txt,sha256=kQzG20CxGf-nSsHmtXHx3hY2-8zHA3jYg8jk0TajqXc,8
338
- dv_pipecat_ai-0.0.75.dev870.dist-info/RECORD,,
339
+ dv_pipecat_ai-0.0.82.dev807.dist-info/METADATA,sha256=KiRlQndV2W1crKYJlr_ksFAeOJOee9sac40jX_hbyHg,32457
340
+ dv_pipecat_ai-0.0.82.dev807.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
341
+ dv_pipecat_ai-0.0.82.dev807.dist-info/top_level.txt,sha256=kQzG20CxGf-nSsHmtXHx3hY2-8zHA3jYg8jk0TajqXc,8
342
+ dv_pipecat_ai-0.0.82.dev807.dist-info/RECORD,,
Binary file
@@ -0,0 +1,95 @@
1
+ import base64
2
+ import json
3
+ from typing import Optional
4
+
5
+ from pydantic import BaseModel
6
+
7
+ from pipecat.audio.utils import create_default_resampler, pcm_to_ulaw, ulaw_to_pcm
8
+ from pipecat.frames.frames import (
9
+ AudioRawFrame,
10
+ Frame,
11
+ InputAudioRawFrame,
12
+ InputDTMFFrame,
13
+ KeypadEntry,
14
+ StartFrame,
15
+ StartInterruptionFrame,
16
+ TransportMessageFrame,
17
+ TransportMessageUrgentFrame,
18
+ )
19
+ from pipecat.serializers.base_serializer import FrameSerializer, FrameSerializerType
20
+
21
+
22
+ class GenesysFrameSerializer(FrameSerializer):
23
+ class InputParams(BaseModel):
24
+ genesys_sample_rate: int = 8000 # Default Genesys rate (8kHz)
25
+ sample_rate: Optional[int] = None # Pipeline input rate
26
+
27
+ def __init__(self, session_id: str, params: InputParams = InputParams()):
28
+ self._session_id = session_id
29
+ self._params = params
30
+ self._genesys_sample_rate = self._params.genesys_sample_rate
31
+ self._sample_rate = 0 # Pipeline input rate
32
+ self._resampler = create_default_resampler()
33
+ self._seq = 1 # Sequence number for outgoing messages
34
+
35
+ @property
36
+ def type(self) -> FrameSerializerType:
37
+ return FrameSerializerType.TEXT
38
+
39
+ async def setup(self, frame: StartFrame):
40
+ self._sample_rate = self._params.sample_rate or frame.audio_in_sample_rate
41
+
42
+ async def serialize(self, frame: Frame) -> str | bytes | None:
43
+ if isinstance(frame, StartInterruptionFrame):
44
+ answer = {
45
+ "version": "2",
46
+ "type": "clearAudio", # Or appropriate event for interruption
47
+ "seq": self._seq,
48
+ "id": self._session_id,
49
+ }
50
+ self._seq += 1
51
+ return json.dumps(answer)
52
+ elif isinstance(frame, AudioRawFrame):
53
+ data = frame.audio
54
+ # Convert PCM to 8kHz μ-law for Genesys
55
+ serialized_data = await pcm_to_ulaw(
56
+ data, frame.sample_rate, self._genesys_sample_rate, self._resampler
57
+ )
58
+ payload = base64.b64encode(serialized_data).decode("utf-8")
59
+ answer = {
60
+ "version": "2",
61
+ "type": "audio",
62
+ "seq": self._seq,
63
+ "id": self._session_id,
64
+ "media": {
65
+ "payload": payload,
66
+ "format": "PCMU",
67
+ "rate": self._genesys_sample_rate,
68
+ },
69
+ }
70
+ self._seq += 1
71
+ return json.dumps(answer)
72
+ elif isinstance(frame, (TransportMessageFrame, TransportMessageUrgentFrame)):
73
+ return json.dumps(frame.message)
74
+
75
+ async def deserialize(self, data: str | bytes) -> Frame | None:
76
+ message = json.loads(data)
77
+ if message.get("type") == "audio":
78
+ payload_base64 = message["media"]["payload"]
79
+ payload = base64.b64decode(payload_base64)
80
+ # Convert Genesys 8kHz μ-law to PCM at pipeline input rate
81
+ deserialized_data = await ulaw_to_pcm(
82
+ payload, self._genesys_sample_rate, self._sample_rate, self._resampler
83
+ )
84
+ audio_frame = InputAudioRawFrame(
85
+ audio=deserialized_data, num_channels=1, sample_rate=self._sample_rate
86
+ )
87
+ return audio_frame
88
+ elif message.get("type") == "dtmf":
89
+ digit = message.get("dtmf", {}).get("digit")
90
+ try:
91
+ return InputDTMFFrame(KeypadEntry(digit))
92
+ except ValueError:
93
+ return None
94
+ else:
95
+ return None
@@ -0,0 +1,45 @@
1
+ import asyncio
2
+ import os
3
+
4
+ from pipecat.frames.frames import TTSAudioRawFrame
5
+ from pipecat.services.google.tts import GoogleTTSService
6
+
7
+
8
+ async def test_chirp_tts():
9
+ # Get credentials from environment variable
10
+ credentials_path = (
11
+ "/Users/kalicharanvemuru/Documents/Code/pipecat/examples/ringg-chatbot/creds.json"
12
+ )
13
+
14
+ if not credentials_path or not os.path.exists(credentials_path):
15
+ raise ValueError(
16
+ "Please set GOOGLE_APPLICATION_CREDENTIALS environment variable to your service account key file"
17
+ )
18
+
19
+ # Initialize the TTS service with Chirp voice
20
+ tts = GoogleTTSService(
21
+ credentials_path=credentials_path,
22
+ voice_id="en-US-Chirp3-HD-Charon", # Using Chirp3 HD Charon voice
23
+ sample_rate=24000,
24
+ )
25
+
26
+ # Test text
27
+ test_text = "Hello, this is a test of the Google TTS service with Chirp voice."
28
+
29
+ print(f"Testing TTS with text: {test_text}")
30
+
31
+ # Generate speech
32
+ try:
33
+ async for frame in tts.run_tts(test_text):
34
+ if isinstance(frame, TTSAudioRawFrame):
35
+ print(f"Received audio chunk of size: {len(frame.audio)} bytes")
36
+ else:
37
+ print(f"Received frame: {frame.__class__.__name__}")
38
+
39
+ print("TTS generation completed successfully!")
40
+ except Exception as e:
41
+ print(f"Error during TTS generation: {str(e)}")
42
+
43
+
44
+ if __name__ == "__main__":
45
+ asyncio.run(test_chirp_tts())
@@ -0,0 +1,5 @@
1
+ """Vistaar AI service implementations."""
2
+
3
+ from .llm import VistaarLLMService
4
+
5
+ __all__ = ["VistaarLLMService"]
@@ -0,0 +1,377 @@
1
+ """Vistaar LLM Service implementation."""
2
+
3
+ import asyncio
4
+ import json
5
+ import time
6
+ import uuid
7
+ from dataclasses import dataclass
8
+ from typing import Any, AsyncGenerator, Dict, Optional
9
+ from urllib.parse import urlencode
10
+
11
+ import httpx
12
+ from loguru import logger
13
+ from pydantic import BaseModel, Field
14
+
15
+ from pipecat.frames.frames import (
16
+ Frame,
17
+ LLMFullResponseEndFrame,
18
+ LLMFullResponseStartFrame,
19
+ LLMMessagesFrame,
20
+ LLMTextFrame,
21
+ LLMUpdateSettingsFrame,
22
+ StartInterruptionFrame,
23
+ StopInterruptionFrame,
24
+ )
25
+ from pipecat.processors.aggregators.llm_response import (
26
+ LLMAssistantAggregatorParams,
27
+ LLMUserAggregatorParams,
28
+ )
29
+ from pipecat.processors.aggregators.openai_llm_context import (
30
+ OpenAILLMContext,
31
+ OpenAILLMContextFrame,
32
+ )
33
+ from pipecat.services.openai.llm import (
34
+ OpenAIAssistantContextAggregator,
35
+ OpenAIContextAggregatorPair,
36
+ OpenAIUserContextAggregator,
37
+ )
38
+ from pipecat.processors.frame_processor import FrameDirection
39
+ from pipecat.services.llm_service import LLMService
40
+
41
+
42
+ class VistaarLLMService(LLMService):
43
+ """A service for interacting with Vistaar's voice API using Server-Sent Events.
44
+
45
+ This service handles text generation through Vistaar's SSE endpoint which
46
+ streams responses in real-time. Vistaar maintains all conversation context
47
+ server-side via session_id, so we only send the latest user message.
48
+ """
49
+
50
+ class InputParams(BaseModel):
51
+ """Input parameters for Vistaar model configuration.
52
+
53
+ Parameters:
54
+ source_lang: Source language code (e.g., 'mr' for Marathi, 'hi' for Hindi).
55
+ target_lang: Target language code for responses.
56
+ session_id: Session ID for maintaining conversation context.
57
+ extra: Additional model-specific parameters.
58
+ """
59
+
60
+ source_lang: Optional[str] = Field(default="mr")
61
+ target_lang: Optional[str] = Field(default="mr")
62
+ session_id: Optional[str] = Field(default=None)
63
+ extra: Optional[Dict[str, Any]] = Field(default_factory=dict)
64
+
65
+ def __init__(
66
+ self,
67
+ *,
68
+ base_url: str = "https://vistaar.kenpath.ai/api",
69
+ params: Optional[InputParams] = None,
70
+ timeout: float = 30.0,
71
+ **kwargs,
72
+ ):
73
+ """Initialize Vistaar LLM service.
74
+
75
+ Args:
76
+ base_url: The base URL for Vistaar API. Defaults to "https://vistaar.kenpath.ai/api".
77
+ params: Input parameters for model configuration and behavior.
78
+ timeout: Request timeout in seconds. Defaults to 30.0 seconds.
79
+ **kwargs: Additional arguments passed to the parent LLMService.
80
+ """
81
+ super().__init__(**kwargs)
82
+
83
+ params = params or VistaarLLMService.InputParams()
84
+
85
+ self._base_url = base_url.rstrip("/")
86
+ self._source_lang = params.source_lang
87
+ self._target_lang = params.target_lang
88
+ self._session_id = params.session_id or str(uuid.uuid4())
89
+ self._extra = params.extra if isinstance(params.extra, dict) else {}
90
+ self._timeout = timeout
91
+
92
+ # Create an async HTTP client
93
+ self._client = httpx.AsyncClient(timeout=httpx.Timeout(self._timeout))
94
+
95
+ # Interruption handling state
96
+ self._current_response = None # Track current HTTP response stream
97
+ self._is_interrupted = False # Track if current generation was interrupted
98
+ self._partial_response = [] # Track what was actually sent before interruption
99
+
100
+ logger.info(
101
+ f"Vistaar LLM initialized - Base URL: {self._base_url}, Session ID: {self._session_id}, Source Lang: {self._source_lang}, Target Lang: {self._target_lang}, Timeout: {self._timeout}s"
102
+ )
103
+
104
+ async def _extract_messages_to_query(self, context: OpenAILLMContext) -> str:
105
+ """Extract only the last user message from context.
106
+
107
+ Since Vistaar maintains context server-side via session_id,
108
+ we only need to send the most recent user message.
109
+
110
+ As a fallback for context synchronization, we can optionally include
111
+ information about interrupted responses.
112
+
113
+ Args:
114
+ context: The OpenAI LLM context containing messages.
115
+
116
+ Returns:
117
+ The last user message as a query string, optionally with context hints.
118
+ """
119
+ messages = context.get_messages()
120
+ query_parts = []
121
+
122
+ # Include interrupted response context as a hint (optional fallback strategy)
123
+ if hasattr(self, "_last_interrupted_response"):
124
+ interrupted_text = self._last_interrupted_response[:100] # Limit length
125
+ query_parts.append(
126
+ f"[Context: I was previously saying '{interrupted_text}...' when interrupted]"
127
+ )
128
+ # Clear the interrupted response after using it
129
+ delattr(self, "_last_interrupted_response")
130
+
131
+ # Find the last user message (iterate in reverse for efficiency)
132
+ for message in reversed(messages):
133
+ if message.get("role") == "user":
134
+ content = message.get("content", "")
135
+
136
+ # Handle content that might be a list (for multimodal messages)
137
+ if isinstance(content, list):
138
+ text_parts = [
139
+ item.get("text", "") for item in content if item.get("type") == "text"
140
+ ]
141
+ content = " ".join(text_parts)
142
+
143
+ if isinstance(content, str):
144
+ query_parts.append(content.strip())
145
+ break
146
+
147
+ # If no user message found, return empty string or just context
148
+ return " ".join(query_parts) if query_parts else ""
149
+
150
+ async def _handle_interruption(self):
151
+ """Handle interruption by cancelling ongoing stream."""
152
+ logger.debug("Handling interruption for Vistaar LLM")
153
+
154
+ # Set interruption flag
155
+ self._is_interrupted = True
156
+
157
+ # Cancel ongoing HTTP response stream if active
158
+ if self._current_response:
159
+ try:
160
+ await self._current_response.aclose()
161
+ logger.debug("Closed active Vistaar response stream")
162
+ except Exception as e:
163
+ logger.warning(f"Error closing Vistaar response stream: {e}")
164
+ finally:
165
+ self._current_response = None
166
+
167
+ # Store partial response for potential inclusion in next query
168
+ if self._partial_response:
169
+ partial_text = "".join(self._partial_response)
170
+ logger.debug(f"Storing interrupted response: {partial_text[:100]}...")
171
+ # Store the interrupted response for next query context
172
+ self._last_interrupted_response = partial_text
173
+
174
+ # Clear current partial response
175
+ self._partial_response = []
176
+
177
+ async def _stream_response(self, query: str) -> AsyncGenerator[str, None]:
178
+ """Stream response from Vistaar API using Server-Sent Events.
179
+
180
+ Args:
181
+ query: The user's query to send to the API.
182
+
183
+ Yields:
184
+ Text chunks from the streaming response.
185
+ """
186
+ # Prepare query parameters
187
+ params = {
188
+ "query": query,
189
+ "session_id": self._session_id,
190
+ "source_lang": self._source_lang,
191
+ "target_lang": self._target_lang,
192
+ }
193
+
194
+ # Add any extra parameters
195
+ params.update(self._extra)
196
+
197
+ # Construct the full URL with query parameters
198
+ url = f"{self._base_url}/voice/?{urlencode(params)}"
199
+
200
+ logger.info(
201
+ f"Vistaar API request - URL: {self._base_url}/voice/, Session: {self._session_id}, Query: {query[:100]}..."
202
+ )
203
+ logger.debug(f"Full URL with params: {url}")
204
+
205
+ # Reset interruption state and partial response for new request
206
+ self._is_interrupted = False
207
+ self._partial_response = []
208
+
209
+ try:
210
+ # Use httpx to handle SSE streaming
211
+ async with self._client.stream("GET", url) as response:
212
+ self._current_response = response # Store for potential cancellation
213
+ response.raise_for_status()
214
+
215
+ # Process the SSE stream
216
+ async for line in response.aiter_lines():
217
+ # Check for interruption before processing each line
218
+ if self._is_interrupted:
219
+ logger.debug("Stream interrupted, stopping processing")
220
+ break
221
+
222
+ if not line:
223
+ continue
224
+
225
+ self._partial_response.append(line) # Track what we're sending
226
+ yield line
227
+
228
+ except httpx.HTTPStatusError as e:
229
+ logger.error(
230
+ f"Vistaar HTTP error - Status: {e.response.status_code}, URL: {url}, Response: {e.response.text if hasattr(e.response, 'text') else 'N/A'}"
231
+ )
232
+ raise
233
+ except httpx.TimeoutException as e:
234
+ logger.error(f"Vistaar timeout error - URL: {url}, Timeout: {self._timeout}s")
235
+ raise
236
+ except Exception as e:
237
+ logger.error(
238
+ f"Vistaar unexpected error - Type: {type(e).__name__}, Message: {str(e)}, URL: {url}"
239
+ )
240
+ raise
241
+ finally:
242
+ # Clean up response reference
243
+ self._current_response = None
244
+
245
+ async def _process_context(self, context: OpenAILLMContext):
246
+ """Process the LLM context and generate streaming response.
247
+
248
+ Args:
249
+ context: The OpenAI LLM context containing messages to process.
250
+ """
251
+ logger.info(f"Vistaar processing context - Session: {self._session_id}")
252
+ try:
253
+ # Extract query from context
254
+ query = await self._extract_messages_to_query(context)
255
+
256
+ if not query:
257
+ logger.warning(
258
+ f"Vistaar: No query extracted from context - Session: {self._session_id}"
259
+ )
260
+ return
261
+
262
+ logger.info(f"Vistaar extracted query: {query}")
263
+
264
+ logger.debug(f"Processing query: {query[:100]}...")
265
+
266
+ # Start response
267
+ await self.push_frame(LLMFullResponseStartFrame())
268
+ await self.start_processing_metrics()
269
+ await self.start_ttfb_metrics()
270
+
271
+ first_chunk = True
272
+ full_response = []
273
+
274
+ # Stream the response
275
+ async for text_chunk in self._stream_response(query):
276
+ if first_chunk:
277
+ await self.stop_ttfb_metrics()
278
+ first_chunk = False
279
+
280
+ # Push each text chunk as it arrives
281
+ await self.push_frame(LLMTextFrame(text=text_chunk))
282
+ full_response.append(text_chunk)
283
+
284
+ # No need to update context - Vistaar maintains all context server-side
285
+ # The response has already been sent via LLMTextFrame chunks
286
+
287
+ except Exception as e:
288
+ logger.error(
289
+ f"Vistaar context processing error - Session: {self._session_id}, Error: {type(e).__name__}: {str(e)}"
290
+ )
291
+ import traceback
292
+
293
+ logger.error(f"Vistaar traceback: {traceback.format_exc()}")
294
+ raise
295
+ finally:
296
+ await self.stop_processing_metrics()
297
+ await self.push_frame(LLMFullResponseEndFrame())
298
+
299
+ async def process_frame(self, frame: Frame, direction: FrameDirection):
300
+ """Process frames for LLM completion requests.
301
+
302
+ Handles OpenAILLMContextFrame, LLMMessagesFrame, and LLMUpdateSettingsFrame
303
+ to trigger LLM completions and manage settings.
304
+
305
+ Args:
306
+ frame: The frame to process.
307
+ direction: The direction of frame processing.
308
+ """
309
+ await super().process_frame(frame, direction)
310
+ context = None
311
+ if isinstance(frame, StartInterruptionFrame):
312
+ await self._handle_interruption()
313
+ await self.push_frame(frame, direction)
314
+ return
315
+ elif isinstance(frame, OpenAILLMContextFrame):
316
+ context = frame.context
317
+ elif isinstance(frame, LLMMessagesFrame):
318
+ context = OpenAILLMContext.from_messages(frame.messages)
319
+ elif isinstance(frame, LLMUpdateSettingsFrame):
320
+ # Update settings if needed
321
+ settings = frame.settings
322
+ if "source_lang" in settings:
323
+ self._source_lang = settings["source_lang"]
324
+ if "target_lang" in settings:
325
+ self._target_lang = settings["target_lang"]
326
+ if "session_id" in settings:
327
+ self._session_id = settings["session_id"]
328
+ logger.debug(f"Updated Vistaar settings: {settings}")
329
+ else:
330
+ await self.push_frame(frame, direction)
331
+
332
+ if context:
333
+ try:
334
+ await self._process_context(context)
335
+ except httpx.TimeoutException:
336
+ logger.error("Timeout while processing Vistaar request")
337
+ await self._call_event_handler("on_completion_timeout")
338
+ except Exception as e:
339
+ logger.error(f"Error processing Vistaar request: {e}")
340
+ raise
341
+
342
+ def create_context_aggregator(
343
+ self,
344
+ context: OpenAILLMContext,
345
+ *,
346
+ user_params: LLMUserAggregatorParams = LLMUserAggregatorParams(),
347
+ assistant_params: LLMAssistantAggregatorParams = LLMAssistantAggregatorParams(),
348
+ ) -> OpenAIContextAggregatorPair:
349
+ """Create context aggregators for Vistaar LLM.
350
+
351
+ Since Vistaar uses OpenAI-compatible message format, we reuse OpenAI's
352
+ context aggregators directly, similar to how Groq and Azure services work.
353
+
354
+ Args:
355
+ context: The LLM context to create aggregators for.
356
+ user_params: Parameters for user message aggregation.
357
+ assistant_params: Parameters for assistant message aggregation.
358
+
359
+ Returns:
360
+ OpenAIContextAggregatorPair: A pair of OpenAI context aggregators,
361
+ compatible with Vistaar's OpenAI-like message format.
362
+ """
363
+ context.set_llm_adapter(self.get_llm_adapter())
364
+ user = OpenAIUserContextAggregator(context, params=user_params)
365
+ assistant = OpenAIAssistantContextAggregator(context, params=assistant_params)
366
+ return OpenAIContextAggregatorPair(_user=user, _assistant=assistant)
367
+
368
+ async def close(self):
369
+ """Close the HTTP client when the service is destroyed."""
370
+ await self._client.aclose()
371
+
372
+ def __del__(self):
373
+ """Ensure the client is closed on deletion."""
374
+ try:
375
+ asyncio.create_task(self._client.aclose())
376
+ except:
377
+ pass