This document provides a summary of how to use the OpenAI API in Python with examples. It includes:
1) Instructions on installing the OpenAI Python package and setting the API key.
2) Examples of different API calls like text completion, sentiment analysis, and image generation along with the JSON response formats.
3) A table summarizing the common arguments for OpenAI API calls like model, prompt, temperature and their effects.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
195 views
Finxter OpenAI Python API
This document provides a summary of how to use the OpenAI API in Python with examples. It includes:
1) Instructions on installing the OpenAI Python package and setting the API key.
2) Examples of different API calls like text completion, sentiment analysis, and image generation along with the JSON response formats.
3) A table summarizing the common arguments for OpenAI API calls like model, prompt, temperature and their effects.
Example Sentiment Analysis Arguments Python OpenAI API Call
prompt = """Do sentiment analysis on the ✓ model: Specifies the model version, e.g., 'gpt-4.0-turbo'. following text. Text: 'Oh, I just adore how ✓ prompt: The input text for the model to process (e.g., question) the sun shines so brightly at 5 a.m., waking ✓ max_tokens: Maximum tokens in the response. Roughly me up every single morning!'""" equates to number of words. ✓ temperature: Controls output randomness (0 to 1). Higher response = openai.Completion.create( value leads to more random replies. engine="text-davinci-003", ✓ top_p: Nucleus sampling strategy (0 to 1). Model will only prompt=prompt, consider subset of tokens whose probability exceeds top_p. max_tokens=200, ✓ n: Number of independent completions to explore. n=1, ✓ stream: Use streaming mode (True or False) to return results stop=None, incrementally (e.g., for real-time apps). temperature=0.5 ✓ echo: Include input prompt in output (True or False). ) ✓ stop: Stopping sequence(s) for generation (string or list of strings). sentiment = response.choices[0].text.strip() ✓ presence_penalty: Penalizes similar tokens in output. print(sentiment) ✓ frequency_penalty: Penalizes frequent tokens in output. # Sentiment: Positive