Eps 2 - ChatGPT and Streamlit

icon picker
Sec 1 - Exploring OpenAI API

OpenAI capabilities

Before proceeding further, it's worth noting the various possibilities of OpenAI. At a high level, the OpenAI API provides an interface to the following product offerings via its API endpoints using either (1) curl or (2) openai Python library:
Text. Generative Pre-Trained Transformers (GPT) can generate LLM responses in the form of document text, computer code, answers to questions, conversational text, and so on by accepting user-provided inputs (prompts). OpenAI provides different flavors of GPT, particularly GPT3, GPT3.5 (the engine driving ChatGPT), and GPT4.
Image. The DALL·E model can generate, manipulate, or create variations of images as guided by input prompts.
Embeddings. Text embeddings provide a numerical representation of text that can be used for semantic search, clustering, recommendations, classification, anomaly detection, and so on. OpenAI's text-embedding-ada-002 provides this capability.
Speech to text. The Whisper model enables the transcription and translation of user-provided audio files through its API endpoints.
Fine-tuning. OpenAI models can be fine-tuned for better results by supplying a foundational model with a compilation of training instances, effectively offering a larger volume of examples than what can be achieved by few-shot learning (i.e., prompts with a few training examples).

GPT for Text Generation

OpenAI refers to text generation as "completions," specifically text completion. This naming convention stems from how language models generate text by using word probability, one word at a time, to complete initial starting words and form complete sentences.
An alternative to completions is "chat completions"—GPT models optimized for conversational text. You may be most familiar with this GPT type, as the underlying GPT 3.5 and their flagship GPT 4 are powering the very popular ChatGPT.
A benefit of chat completions is that they’re less prone to prompt injection attacks, as user-provided content is separate from instruction prompts.

Get your own OpenAI API key

Follow these steps to obtain your API key from OpenAI:
Click on Menu > Developers > Overview
Click on your Profile image (top right) > View API keys
Click on + Create new secret key
Optionally, enter a name for the API key for future reference
That's all you need to do to create your own OpenAI API key, which will begin with sk-.
Loading…
For more information on safely using API keys, read this
.

Making an API request

Once you have an API key set up, the final step is to send your first API request. To do so, a sample request to the , , and API are included below. Because the API key was set in Step 2, it should be automatically referenced via $OPENAI_API_KEY in your terminal or command line. You can also manually replace $OPENAI_API_KEY with your API key but be sure to keep the curl command hidden if it includes your API key.
ChatCompletions
curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."
},
{
"role": "user",
"content": "Compose a poem that explains the concept of recursion in programming."
}
]
}'
The example highlights just one area of strength for our models: creative ability. Explaining recursion (the programming topic) in a well formatted poem is something both the best developers and best poets would struggle with. In this case, gpt-3.5-turbo does it effortlessly.
This link can't be embedded.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.