QChat is a simpler way to use the OpenAI's, Ollama's and Maritaca AI's APIs for interacting with the models easily and for developing small agents.
If you want to use this project and want a better "tutorial", I recommend you to see the classes and the functions in the qchat.py and example.py codes. The codes are not big and are easy to understand.
from qchat import MaritacaAI
chat = MaritacaAI('secret',
'https://chat.maritaca.ai/api',
'sabiazinho-3', 12000)
chat.add_system_role('Show the factorial of the number which the user will request.')
chat.add_user_question('5')
chat.add_assistant_response('120')
chat.add_user_question('What is the result that you have just shown?')
chat.complete()
print(chat) # This will show the entire chat!This library is very simple as I have told you. Let us take a look!
Follow this example
chat = QChat('apis_secret',
'apis_url',
'apis_chosen_model', 12000) # The last parameter is the maximum value of tokens in which the chosen model may use.QChat has some different functionalities as shown bellow.
chat.add_system_role('Show the factorial of the number which the user will request.')
chat.add_user_question('5')
chat.add_assistant_response('120')
chat.add_user_question('What is the result that you have just shown?')
chat.complete() # This will complete the chat with the model's response
# These functions give a consecutive array of the messages.
print(chat.get_consecutive_roles_array())
print(chat.get_consecutive_messages_array())
print(chat) # This will print all chat's messages!From those get_consecutive_array functions:
['system', 'user', 'assistant', 'user', 'assistant']
['Show the factorial of the number which the user will request.', '5', '120', 'Which is the result that you have just shown?', 'The factorial of 5 is 120.']From the print(chat) functionality:
System:
Show the factorial of the number which the user will request.
User:
5
Assistant:
120
User:
Which is the result that you have just shown?
Assistant:
The factorial of 5 is 120.
There are two functions to calculate the total of tokens and the estimate cost for Sábia-4 and Sabiazinho-4 models. This is the class:
class MaritacaAI(OpenAI):
prices = {
"sabia-4": {"input": 5/1000000, "output": 20/1000000},
"sabiazinho-4": {"input": 1/1000000, "output": 4/1000000},
}
def count_spended_tokens(self):
total = 0
for i in self.messages:
total += count_tokens(i['content'], model=self.model)
return total
def calculate_estimated_cost(self):
return ((self.prices[self.model]["input"] + self.prices[self.model]["output"])/2) * self.count_spended_tokens()Thank you for your attention!