├── README.md ├── user.png ├── lechat.png ├── assistant.ico ├── assistant.png ├── mistralai.png ├── mistral-banner.png ├── chatMistral_terminal.py └── chatMistral_Stream.py /README.md: -------------------------------------------------------------------------------- 1 | # Mistral-LeChat 2 | all you need to start with Le Chat Mistral API 3 | -------------------------------------------------------------------------------- /user.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/Mistral-LeChat/main/user.png -------------------------------------------------------------------------------- /lechat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/Mistral-LeChat/main/lechat.png -------------------------------------------------------------------------------- /assistant.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/Mistral-LeChat/main/assistant.ico -------------------------------------------------------------------------------- /assistant.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/Mistral-LeChat/main/assistant.png -------------------------------------------------------------------------------- /mistralai.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/Mistral-LeChat/main/mistralai.png -------------------------------------------------------------------------------- /mistral-banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fabiomatricardi/Mistral-LeChat/main/mistral-banner.png -------------------------------------------------------------------------------- /chatMistral_terminal.py: -------------------------------------------------------------------------------- 1 | from mistralai import Mistral 2 | import sys 3 | import warnings 4 | warnings.filterwarnings(action='ignore') 5 | import os 6 | 7 | def clear_screen(): 8 | os.system('cls' if os.name == 'nt' else 'clear') 9 | 10 | clear_screen() 11 | print("\033[94;1m") #light blue bold 12 | APIK = input('Your Mistral API key: ') # 32 char from https://console.mistral.ai/api-keys/ 13 | CLIENT = Mistral(api_key=APIK) 14 | history = [] 15 | 16 | print("\033[93;1m") #light yellow 17 | intro = """ 18 | 19 | ░░░ ░░ 20 | ▒█▒░░░░ ░█▓░░░░ 21 | ▒█▒░░░░ ░█▓░░░░ 22 | ▒█▒░░░░░░░░ ▒█▒░░░░░░░░ ▓▓ ▓▓▒▒▒ ░▓▓▓▓░▓▒ █▒ ▓█▓ ░▓▓█▓▒ 23 | ▒█▒░░░░░░░░ ▒█▒░░░░░░░░ ▓▓ ▓▓▒▒░ █▒ ▓▓▒▒█▒ ░█▒█░ ▒▓ 24 | ▒█▒▒▒▒▒▒▒░░░░▒▓▒▒▒▒▒▒▒░░ ▓▓ ▓▒ ▓▒ ▒ ▓▓ ░█▒ ▓█▓█▒ ▒▓ 25 | ▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▓▓▓▓ ▓▓▓▓▓ ▓▓▓▒ ▓▒ ▓▒░▓ ░▓░ ▒▓ 26 | ▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 27 | ▒█▓▒▒▒░ ▓█▒▒▒▒░ ░█▓▒▒▒▒ 28 | ▒█▓▒▒▒░ ▓█▒▒▒▒░ ░█▓▒▒▒▒ ▓█░▓█ ░▓██▓ ▓▓▓▓ ▒▓▓█▓▓░█▓▓▓▒ ▓▓▒ ░█░ 29 | ▒█▓▒▒▒░ ░▒░░░░ ░▓▓▒▒▒▒ ▓▒▓▓▓ ▓▓ ░█▓▒ ▒█ █▒ ▓▓ ░█░▓░ ░█░ 30 | ▒█▓▒▒▒▒ ▒▓ ░▓░ ░█▓▒▒▒▒ ▓▒▓░▓ ▓▓ ░▓▒ ▒█ █▒▒▓ ▓█▓█▓ ░█░ 31 | ▒█▓▒▒▒▒ ▓█ ▒█▒ ░▓▓▒▒▒▒ ▒▒ ▓ ░▓▓▓▓ ░▓▓▓▒ ▒▓ ▓░ ▒▓░▓░ ░▓░░▓▓▓▓░ 32 | 33 | """ 34 | print(intro) 35 | 36 | def chatMistral(CLIENT,history,prompt): 37 | """ 38 | SDK call to Mistral Platforme endpoints for free and paid models 39 | The function will NOT STREAM the output 40 | - it is required a verified API key from https://console.mistral.ai/ 41 | inputs: CLIENT -> instance of Mistral object 42 | history -> list, chat messages 43 | prompt -> str, the prompt from the user 44 | outputs : history -> list, chat messages including the prompt and model response 45 | answer -> str, the model response to the prompt 46 | Usage example: 47 | from mistralai import Mistral 48 | CLIENT = Mistral(api_key=APIK) 49 | history = [] 50 | history, new_message = chatMistral(CLIENT,history,'what is Science?') 51 | --- 52 | more examples from https://docs.mistral.ai/capabilities/completion/ 53 | """ 54 | model = 'mistral-small-latest' 55 | history.append({"role": "user","content": prompt}) 56 | response = CLIENT.chat.complete(model=model, 57 | messages = history) 58 | answer = response.choices[0].message.content 59 | history.append({"role": "assistant", "content": answer}) 60 | return history,answer 61 | 62 | while True: 63 | userinput = "" 64 | print("\033[1;30m") #dark grey 65 | print("Enter your text (end input with Ctrl+D on Unix or Ctrl+Z on Windows) - type quit! to exit the chatroom:") 66 | print("\033[91;1m") #red 67 | lines = sys.stdin.readlines() 68 | for line in lines: 69 | userinput += line + "\n" 70 | if "quit!" in lines[0].lower(): 71 | print("\033[0mBYE BYE!") 72 | break 73 | print("\033[92;1m") 74 | history, new_message = chatMistral(CLIENT,history,userinput) 75 | print(new_message) 76 | -------------------------------------------------------------------------------- /chatMistral_Stream.py: -------------------------------------------------------------------------------- 1 | from mistralai import Mistral 2 | import sys 3 | import warnings 4 | warnings.filterwarnings(action='ignore') 5 | import os 6 | 7 | def clear_screen(): 8 | os.system('cls' if os.name == 'nt' else 'clear') 9 | 10 | clear_screen() 11 | print("\033[94;1m") #light blue bold 12 | APIK = input('Your Mistral API key: ') # 32 char from https://console.mistral.ai/api-keys/ 13 | CLIENT = Mistral(api_key=APIK) 14 | history = [] 15 | 16 | print("\033[93;1m") #light yellow 17 | intro = """ 18 | 19 | ░░░ ░░ 20 | ▒█▒░░░░ ░█▓░░░░ 21 | ▒█▒░░░░ ░█▓░░░░ 22 | ▒█▒░░░░░░░░ ▒█▒░░░░░░░░ ▓▓ ▓▓▒▒▒ ░▓▓▓▓░▓▒ █▒ ▓█▓ ░▓▓█▓▒ 23 | ▒█▒░░░░░░░░ ▒█▒░░░░░░░░ ▓▓ ▓▓▒▒░ █▒ ▓▓▒▒█▒ ░█▒█░ ▒▓ 24 | ▒█▒▒▒▒▒▒▒░░░░▒▓▒▒▒▒▒▒▒░░ ▓▓ ▓▒ ▓▒ ▒ ▓▓ ░█▒ ▓█▓█▒ ▒▓ 25 | ▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▓▓▓▓ ▓▓▓▓▓ ▓▓▓▒ ▓▒ ▓▒░▓ ░▓░ ▒▓ 26 | ▒█▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 27 | ▒█▓▒▒▒░ ▓█▒▒▒▒░ ░█▓▒▒▒▒ 28 | ▒█▓▒▒▒░ ▓█▒▒▒▒░ ░█▓▒▒▒▒ ▓█░▓█ ░▓██▓ ▓▓▓▓ ▒▓▓█▓▓░█▓▓▓▒ ▓▓▒ ░█░ 29 | ▒█▓▒▒▒░ ░▒░░░░ ░▓▓▒▒▒▒ ▓▒▓▓▓ ▓▓ ░█▓▒ ▒█ █▒ ▓▓ ░█░▓░ ░█░ 30 | ▒█▓▒▒▒▒ ▒▓ ░▓░ ░█▓▒▒▒▒ ▓▒▓░▓ ▓▓ ░▓▒ ▒█ █▒▒▓ ▓█▓█▓ ░█░ 31 | ▒█▓▒▒▒▒ ▓█ ▒█▒ ░▓▓▒▒▒▒ ▒▒ ▓ ░▓▓▓▓ ░▓▓▓▒ ▒▓ ▓░ ▒▓░▓░ ░▓░░▓▓▓▓░ 32 | 33 | """ 34 | print(intro) 35 | 36 | def chatMistral(CLIENT,history,prompt): 37 | """ 38 | SDK call to Mistral Platforme endpoints for free and paid models 39 | The function will STREAM the output token by token 40 | - it is required a verified API key from https://console.mistral.ai/ 41 | inputs: CLIENT -> instance of Mistral object 42 | history -> list, chat messages 43 | prompt -> str, the prompt from the user 44 | outputs : history -> list, chat messages including the prompt and model response 45 | answer -> str, the model response to the prompt 46 | Usage example: 47 | from mistralai import Mistral 48 | CLIENT = Mistral(api_key=APIK) 49 | history = [] 50 | history, new_message = chatMistral(CLIENT,history,'what is Science?') 51 | --- 52 | more examples from https://docs.mistral.ai/capabilities/completion/ 53 | """ 54 | model = 'mistral-small-latest' 55 | history.append({"role": "user","content": prompt}) 56 | temp = 0.1 57 | pp = 1.2 58 | maxtokens = 1000 59 | res = CLIENT.chat.stream(model=model, 60 | messages = history, temperature=temp, presence_penalty=pp, max_tokens=maxtokens) 61 | answer = '' 62 | for chunk in res: 63 | if chunk.data.choices[0].delta.content is not None: 64 | print(chunk.data.choices[0].delta.content, end="") 65 | answer = answer + chunk.data.choices[0].delta.content 66 | history.append({"role": "assistant", "content": answer}) 67 | return history,answer 68 | 69 | while True: 70 | userinput = "" 71 | print("\033[1;30m") #dark grey 72 | print("Enter your text (end input with Ctrl+D on Unix or Ctrl+Z on Windows) - type quit! to exit the chatroom:") 73 | print("\033[91;1m") #red 74 | lines = sys.stdin.readlines() 75 | for line in lines: 76 | userinput += line + "\n" 77 | if "quit!" in lines[0].lower(): 78 | print("\033[0mBYE BYE!") 79 | break 80 | print("\033[92;1m") 81 | history, new_message = chatMistral(CLIENT,history,userinput) 82 | --------------------------------------------------------------------------------