#How use CosmoRP api with python
1 messages Β· Page 1 of 1 (latest)
For this, you can use the openai library
pip install openai```
then you can import it at your code and use it
from openai import OpenAI
first, you need to set the key and the base url. CosmoRP is free to use and you dont need a key, so I'll leave the key as "no key"
client = OpenAI(
base_url = "https://api.pawan.krd/cosmosrp/v1/chat/completions",
api_key = "no key"
)
Then, you can make a request and print the response.
That's all
Think you, it finally worked, I thought it should be like with a regular openai
Yrw
To make the AI remember the past messages they must be in an array of objects with role and content properties
[
{
"role": "user",
"content": "hello!!!"
},
{
"role": "assistant",
"content": "Hello, how can I assist you today?"
},
{
"role": "user",
"content": "teach me how to code in python!"
},
]```
like this
That is, you need to add the old one to the new request?
Yes
thank you, now i go think plan(for my project)
Yrw and good luck.
I don't know why, but he just ignores past messages
Can you show me the code?
The chat history is not being handled correctly. The chat list should be initialized with the system message, and then the user and AI messages should be appended to it.
The way you're doing it, if I send "hello", the chat would be:
[{'content': 'about character', 'role': 'system'},
[{'content': 'hello', 'role': 'user'}],
[{'content': "AI response"]}
``` Really messy, as I said, messages must be an array of objects
just remove the brackets in
def st(mess):
chatuser = [{
"role": "user",
"content": mess
}]```
and
```py
chatai = [{
"role": response.choices[0].message.role,
"content": response.choices[0].message.content
}]