#How use CosmoRP api with python

1 messages Β· Page 1 of 1 (latest)

languid gulch
#

I want to use ,CosmoRP using Python. How to do it? And how can I make the bot remember past messages, etc.?

lean dune
#

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

languid gulch
#

Think you, it finally worked, I thought it should be like with a regular openai

lean dune
#

Yrw

languid gulch
lean dune
#

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

languid gulch
#

That is, you need to add the old one to the new request?

lean dune
#

Yes

languid gulch
#

thank you, now i go think plan(for my project)

lean dune
#

Yrw and good luck.

languid gulch
#

I don't know why, but he just ignores past messages

lean dune
#

Can you show me the code?

languid gulch
#

Yes, can, wait 5 minutes

lean dune
#

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
    }]