7
7
from clibot .config import *
8
8
from clibot .colors import *
9
9
from openai import AsyncOpenAI
10
+ from functools import lru_cache
10
11
11
12
12
13
package_dir = os .path .dirname (os .path .abspath (__file__ ))
20
21
/clear Clear the conversation history"""
21
22
22
23
24
+ @lru_cache (maxsize = 1 )
23
25
def load_chat_history ():
24
26
if os .path .exists (history_file ):
25
27
try :
@@ -55,18 +57,17 @@ async def chatbot(messages, messages_history, client):
55
57
56
58
try :
57
59
loading .start ()
58
- response = await client .chat .completions .create (
60
+ stream = await client .chat .completions .create (
59
61
model = AI_MODEL ,
60
62
messages = messages_history ,
61
63
temperature = TEMPERATURE ,
62
64
top_p = TOP_P ,
63
65
max_tokens = MAX_TOKENS ,
64
66
stream = True ,
65
- # stop=None
66
67
)
67
68
loading .stop ()
68
69
respond = ""
69
- async for chunk in response :
70
+ async for chunk in stream :
70
71
if chunk .choices [0 ].delta .content :
71
72
content = chunk .choices [0 ].delta .content
72
73
print (content , end = "" , flush = True )
@@ -77,10 +78,9 @@ async def chatbot(messages, messages_history, client):
77
78
print ()
78
79
79
80
except KeyboardInterrupt :
80
- loading .stop ()
81
- streaming_response (f"\n { LIGHT_RED } Exiting...{ COLOR_RESET } " )
82
- exit ()
83
-
81
+ loading .stop ()
82
+ streaming_response (f"\n { LIGHT_RED } Exiting...{ COLOR_RESET } " )
83
+ exit ()
84
84
except Exception as e :
85
85
loading .stop ()
86
86
streaming_response (f"{ LIGHT_RED } Error: { str (e )} { COLOR_RESET } \n " )
@@ -90,31 +90,32 @@ async def chatbot(messages, messages_history, client):
90
90
pass
91
91
92
92
93
- def start_chat ():
94
- try :
95
- query = input (">>> " ).strip () or "/help"
96
- if query == "/bye" :
97
- exit ()
98
- elif query == "/help" :
99
- print (help_msg + "\n " )
100
- elif query == "/clear" :
101
- clear_chat_history ()
102
- else :
103
- if AI_PROVIDER == "groq" :
104
- client = AsyncOpenAI (api_key = API_KEY , base_url = GROQ_AI_ENDPOINT )
105
- elif AI_PROVIDER == "openai" :
106
- client = AsyncOpenAI (api_key = API_KEY )
107
- elif AI_PROVIDER == "mistral" :
108
- client = AsyncOpenAI (api_key = API_KEY , base_url = MISTRAL_AI_ENDPOINT )
109
- elif AI_PROVIDER == "ollama" :
110
- client = AsyncOpenAI (api_key = API_KEY , base_url = OLLAMA_AI_ENDPOINT )
111
- asyncio .run (chatbot ({"role" : "user" , "content" : query }, load_chat_history (), client ))
112
- except KeyboardInterrupt :
113
- print ("\n Exiting..." )
114
- exit ()
115
- except Exception as e :
116
- print (f"\n { LIGHT_RED } Error: { str (e )} { COLOR_RESET } " )
117
- exit ()
118
- except RuntimeError :
119
- pass
93
+ async def start_chat ():
94
+ try :
95
+ query = input (">>> " ).strip () or "/help"
96
+ if query == "/bye" :
97
+ exit ()
98
+ elif query == "/help" :
99
+ print (help_msg + "\n " )
100
+ elif query == "/clear" :
101
+ clear_chat_history ()
102
+ else :
103
+ client = AsyncOpenAI (
104
+ api_key = API_KEY ,
105
+ base_url = {
106
+ "groq" : GROQ_AI_ENDPOINT ,
107
+ "openai" : None ,
108
+ "mistral" : MISTRAL_AI_ENDPOINT ,
109
+ "ollama" : OLLAMA_AI_ENDPOINT
110
+ }.get (AI_PROVIDER )
111
+ )
112
+ await chatbot ({"role" : "user" , "content" : query }, load_chat_history (), client )
113
+ except KeyboardInterrupt :
114
+ print ("\n Exiting..." )
115
+ exit ()
116
+ except Exception as e :
117
+ print (f"\n { LIGHT_RED } Error: { str (e )} { COLOR_RESET } " )
118
+ exit ()
119
+ except RuntimeError :
120
+ pass
120
121
0 commit comments