2
2
import json
3
3
4
4
import openai
5
+ from llama_index .response .schema import StreamingResponse
5
6
6
7
from qtpy .QtCore import QThread , Signal
7
8
@@ -13,15 +14,14 @@ class OpenAIThread(QThread):
13
14
== replyGenerated Signal ==
14
15
First: response
15
16
Second: user or AI
16
- Third: streaming a chat completion or not
17
+ Third: streaming or not streaming
17
18
"""
18
19
replyGenerated = Signal (str , bool
10000
, bool )
19
20
streamFinished = Signal ()
20
21
21
22
def __init__ (self , model , openai_arg , * args , ** kwargs ):
22
23
super ().__init__ (* args , ** kwargs )
23
24
self .__model = model
24
- print (model )
25
25
self .__endpoint = getModelEndpoint (model )
26
26
self .__openai_arg = openai_arg
27
27
@@ -45,15 +45,32 @@ def run(self):
45
45
else :
46
46
response_text = response ['choices' ][0 ]['message' ]['content' ]
47
47
self .replyGenerated .emit (response_text , False , False )
48
- elif self .__endpoint == '/v1/completions' :
49
- openai_object = openai .Completion .create (
50
- ** self .__openai_arg
51
- )
52
-
53
- response_text = openai_object ['choices' ][0 ]['text' ].strip ()
54
- self .replyGenerated .emit (response_text , False , False )
55
48
except openai .error .InvalidRequestError as e :
56
- print (e )
57
49
self .replyGenerated .emit (f'<p style="color:red">{ e } </p>' , False , False )
58
50
except openai .error .RateLimitError as e :
59
- self .replyGenerated .emit (f'<p style="color:red">{ e } <br/>Check the usage: https://platform.openai.com/account/usage<br/>Update to paid account: https://platform.openai.com/account/billing/overview' , False , False )
51
+ self .replyGenerated .emit (f'<p style="color:red">{ e } <br/>Check the usage: https://platform.openai.com/account/usage<br/>Update to paid account: https://platform.openai.com/account/billing/overview' , False , False )
52
+
53
+
54
+ class LlamaOpenAIThread (QThread ):
55
+ replyGenerated = Signal (str , bool , bool )
56
+ streamFinished = Signal ()
57
+
58
+ def __init__ (self , llama_idx_instance , query_text , * args , ** kwargs ):
59
+ super ().__init__ (* args , ** kwargs )
60
+ self .__llama_idx_instance = llama_idx_instance
61
+ self .__query_text = query_text
62
+
63
+ def run (self ):
64
+ try :
65
+ resp = self .__llama_idx_instance .getResponse (self .__query_text )
66
+ f = isinstance (resp , StreamingResponse )
67
+ if f :
68
+ for response_text in resp .response_gen :
69
+ self .replyGenerated .emit (response_text , False , f )
70
+ else :
71
+ self .replyGenerated .emit (resp .response , False , f )
72
+ except openai .error .InvalidRequestError as e :
73
+ self .replyGenerated .emit ('<p style="color:red">Your request was rejected as a result of our safety system.<br/>'
74
+ 'Your prompt may contain text that is not allowed by our safety system.</p>' , False )
75
+ except openai .error .RateLimitError as e :
76
+ self .replyGenerated .emit (f'<p style="color:red">{ e } <br/>Check the usage: https://platform.openai.com/account/usage<br/>Update to paid account: https://platform.openai.com/account/billing/overview' , False )
0 commit comments