Wednesday, April 26, 2023

Simple OpenAi Python Script

 Make your own OpenAi chat


I am totally new comer in Python trying to learn this language from scratch. Knowing a basic of python language is a quite easy for me as it is not a big different from JavaScript (if not mistaken). As the OpenAi are become popular today i take a chance to learn on how to make a simple OpenAi script using Python. 

I usually use Microsoft Visual Studio Code to scripting. We have to install OpenAi extension first by typing this command to the terminal:

pip install openai

then after that, create a new python file named ai.py and put in this codes. Save it and run it using debugger or type python3 ai.py:

Hope this helps

import openai

openai.api_key='get-your-own-key' #generate it from openai.com

while True:
    model_engine ="text-davinci-003" #model 3 openai
    prompt = input("[AI] Apa yang anda ingin tahu? : ")
    if prompt == "exit" or prompt == "quit":
        break

    completion = openai.Completion.create(
        engine = model_engine,
        prompt = prompt,
        max_tokens = 4000, #jumlah maksimum teks,
        n = 1, #tetap hanya sekali respon,
        stop = None,
        temperature = 0 #semakin tinggi akan memberi kesan tidak accurate
    )
    response = completion.choices[0].text

    print(response + "\n")
    print("=="*40)
    print("\n")


No comments:

 Simple Python Calculator This script will allows your to calculate the integers given with the chosen operation. You can add, substract, mu...