Hey there,
I am new to coding using APIs. I wanted to make a simple text chatbot using a chatgpt API. However, I am confused about how I implement my API into my code. I have read the documentation but I am still confused. Could someone please give me a simple text API chatbot as an example and explain with an explanation of how they implemented the api into the code? Thank you so much for your response!
Hello, welcome to the forum.
This is confusing, you’re correct. There are many ways to programmatically make API calls depending on the environment you’re using.
Basically, you can do so directly by passing back and forth the json which the data is in:
curl https://api.openai.com/v1/chat/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer $OPENAI_API_KEY"
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "write a haiku about ai"}
]
}'
But you can also use the OpenAI SDKs for Python or Node.js. These SDKs (Software Developer Kits) make it so you can use that particular coding option to access the API instead.
#Python
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "write a haiku about ai"}
]
)
Both of those are the beginning of how to write a chatbot and do the same thing. The next step is streaming.
1 Like