How to use ChatGPT API?

MisterretsiM

New member
Sep 9, 2022
2
0
1
Is it possible somehow, through webget or something else, to interact with the API from ChatGPT (GPT-3), I need this in order to make a conversation with a virtual friend on the server, and not only for this, I can do this ?, there are just a few examples for python, and there is authorization through the site, but I still could not do anything, I decided to ask here.
 

Hydra

Helper
Staff member
Helper
Jan 26, 2019
23
6
3
30
Is it possible somehow, through webget or something else, to interact with the API from ChatGPT (GPT-3), I need this in order to make a conversation with a virtual friend on the server, and not only for this, I can do this ?, there are just a few examples for python, and there is authorization through the site, but I still could not do anything, I decided to ask here.

Yes; You can use OpenAI's API endpoint using the WebGet command to interact with your server as a virtual friend. What you would need is to have basic understanding of making web requests and understanding the basics of JSON structures.

Per their documentation, this is where you want to reference first:

You can obtain an API key here, if you haven't already:
https://beta.openai.com/account/api-keys

Your URL endpoint is https://api.openai.com/v1/completions

You can define your <[headers]> for the Webget command simply like this:
YAML:
- definemap headers:
    Authorization: <secret[ai]>
    Content-Type: application/json
    User-Agent: your_name

For more information on SecretTags such as the example used above <secret[ai]>, you can reference this part of the meta for setting yours up:
It's very important you never reveal your OpenAI API Token, or to have it written blatantly in your script to be saved in ram. Using a SecretTag allows you to properly secure your token.

Your <[data]> is going to be where your prompt goes, here's a simple example:
YAML:
- definemap data:
    model: text-davinci-003
    prompt: Tell me a joke!
    temperature: 0
    max_tokens: 300

This example uses the text-davinci-003 model. You can learn more about the other models available Here.

Your Webget command will look something like this:
~webget <> data:<[data]> headers:<[headers]> save:response in which, you use your save argument to get the <entry[response].result> result, which you can parse using a tag like the yaml parsing tags.

You can use <entry[response].failed> to determine if the response failed, or not.

You can use <util.parse_yaml[<entry[response].result>].get[choices].first.get[text]> to return the response that resulted from the web request.

I've actually created a Denizen script that implements this into Discord using dDiscordBot if you'd like a working example.

You should also find better help and support on our Discord - my username is @Hydra Melody#0001 if you have questions specifically about my script, but we can help you better there.

Hope this helps.

Edit: fixed my formatting, added a tiny bit more info
 
Last edited:

Luigi Vampa

New member
Mar 22, 2023
1
0
1
The chat api.

Code:
    - definemap headers 'Authorization:Bearer <[apikey]>' Content-Type:application/json User-Agent:TBM
    - definemap data:
        "model": "gpt-3.5-turbo"
        #"temperature": 2
        "messages":
        -
            "role": "system"
            "content": "Pretend you are the bartender of a hotel called Hotel Vampa and you are talking to registered guest called <player.name>."
        -
            "role": "user"
            "content": "<player.chat_history[1]>"

    - ~webget https://api.openai.com/v1/chat/completions data:<[data].to_json> headers:<[headers]> save:response
    - chat <util.parse_yaml[<entry[response].result>].get[choices].first.get[message].get[content]>

The completions api

Code:
    - definemap headers 'Authorization:Bearer <[apikey]>' Content-Type:application/json User-Agent:TBM
    - definemap data model:text-davinci-003 prompt:<player.chat_history[1]>
    - ~webget https://api.openai.com/v1/completions data:<[data].to_json> headers:<[headers]> save:response
    - chat <util.parse_yaml[<entry[response].result>].get[choices].first.get[text]>