File tree 4 files changed +41
-8
lines changed
4 files changed +41
-8
lines changed Original file line number Diff line number Diff line change 1
1
from typing import Any , List , Optional
2
-
3
2
from llama_index .core .bridge .pydantic import BaseModel
4
3
from llama_index .core .base .llms .types import (
5
4
ChatMessage ,
@@ -138,7 +137,7 @@ class LLMChatInProgressEvent(BaseEvent):
138
137
139
138
Args:
140
139
messages (List[ChatMessage]): List of chat messages.
141
- response (ChatResponse): Chat response currently beiung streamed.
140
+ response (ChatResponse): Chat response currently being streamed.
142
141
"""
143
142
144
143
messages : List [ChatMessage ]
@@ -155,11 +154,11 @@ class LLMChatEndEvent(BaseEvent):
155
154
156
155
Args:
157
156
messages (List[ChatMessage]): List of chat messages.
158
- response (ChatResponse): Chat response.
157
+ response (Optional[ ChatResponse] ): Last chat response.
159
158
"""
160
159
161
160
messages : List [ChatMessage ]
162
- response : ChatResponse
161
+ response : Optional [ ChatResponse ]
163
162
164
163
@classmethod
165
164
def class_name (cls ):
Original file line number Diff line number Diff line change @@ -97,7 +97,7 @@ async def wrapped_gen() -> ChatResponseAsyncGen:
97
97
dispatcher .event (
98
98
LLMChatEndEvent (
99
99
messages = messages ,
100
- response = x ,
100
+ response = last_response ,
101
101
span_id = span_id ,
102
102
)
103
103
)
@@ -173,7 +173,7 @@ def wrapped_gen() -> ChatResponseGen:
173
173
dispatcher .event (
174
174
LLMChatEndEvent (
175
175
messages = messages ,
176
- response = x ,
176
+ response = last_response ,
177
177
span_id = span_id ,
178
178
)
179
179
)
Original file line number Diff line number Diff line change 1
1
from typing import Any , Callable , Optional , Sequence
2
-
3
2
from llama_index .core .base .llms .types import (
4
3
ChatMessage ,
4
+ ChatResponseGen ,
5
5
CompletionResponse ,
6
6
CompletionResponseGen ,
7
7
LLMMetadata ,
8
8
)
9
9
from llama_index .core .callbacks import CallbackManager
10
- from llama_index .core .llms .callbacks import llm_completion_callback
10
+ from llama_index .core .llms .callbacks import llm_chat_callback , llm_completion_callback
11
11
from llama_index .core .llms .custom import CustomLLM
12
12
from llama_index .core .types import PydanticProgramMode
13
13
@@ -76,3 +76,11 @@ def gen_response(max_tokens: int) -> CompletionResponseGen:
76
76
)
77
77
78
78
return gen_response (self .max_tokens ) if self .max_tokens else gen_prompt ()
79
+
80
+
81
+ class MockLLMWithNonyieldingChatStream (MockLLM ):
82
+ @llm_chat_callback ()
83
+ def stream_chat (
84
+ self , messages : Sequence [ChatMessage ], ** kwargs : Any
85
+ ) -> ChatResponseGen :
86
+ yield from []
Original file line number Diff line number Diff line change 1
1
import pytest
2
+ from llama_index .core .base .llms .types import ChatMessage
2
3
from llama_index .core .llms .llm import LLM
3
4
from llama_index .core .llms .mock import MockLLM
5
+ from llama_index .core .llms .mock import MockLLMWithNonyieldingChatStream
6
+
7
+
8
+ @pytest .fixture ()
9
+ def nonyielding_llm () -> LLM :
10
+ return MockLLMWithNonyieldingChatStream ()
4
11
5
12
6
13
@pytest .fixture ()
@@ -13,6 +20,25 @@ def prompt() -> str:
13
20
return "test prompt"
14
21
15
22
23
+ def test_llm_stream_chat_handles_nonyielding_stream (
24
+ nonyielding_llm : LLM , prompt : str
25
+ ) -> None :
26
+ response = nonyielding_llm .stream_chat ([ChatMessage (role = "user" , content = prompt )])
27
+ for _ in response :
28
+ pass
29
+
30
+
31
+ @pytest .mark .asyncio ()
32
+ async def test_llm_astream_chat_handles_nonyielding_stream (
33
+ nonyielding_llm : LLM , prompt : str
34
+ ) -> None :
35
+ response = await nonyielding_llm .astream_chat (
36
+ [ChatMessage (role = "user" , content = prompt )]
37
+ )
38
+ async for _ in response :
39
+ pass
40
+
41
+
16
42
def test_llm_complete_prompt_arg (llm : LLM , prompt : str ) -> None :
17
43
res = llm .complete (prompt )
18
44
expected_res_text = prompt
You can’t perform that action at this time.
0 commit comments