Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon GPT-4o API How to create a prompt suggestion App in Python
    GPT-4o API


GPT-4o API How to create a prompt suggestion App in Python


Here is an example of how you can use the GPT-4o API to create a prompt suggestion application in Python:

python
import openai

openai.api_key = "YOUR_API_KEY"

def get_prompt_suggestions(user_input):
    """
    Generates prompt suggestions based on the user's input using the GPT-4o API.
    
    Args:
        user_input (str): The user's input or query.
    
    Returns:
        list: A list of prompt suggestions.
    """
    response = openai.Completion.create(
        engine="gpt-4o",
        prompt=f"User: {user_input}\nAssistant: Here are some prompt suggestions based on your input:",
        max_tokens=100,
        n=5,
        stop=None,
        temperature=0.7,
    )
    
    suggestions = [choice.text.strip() for choice in response.choices]
    return suggestions

def main():
    while True:
        user_input = input("Enter your query (or 'q' to quit): ")
        if user_input.lower() == 'q':
            break
        
        suggestions = get_prompt_suggestions(user_input)
        print("Prompt Suggestions:")
        for i, suggestion in enumerate(suggestions, start=1):
            print(f"{i}. {suggestion}")

if __name__ == "__main__":
    main()

Here's how the application works:

1. The `get_prompt_suggestions()` function takes the user's input and sends it to the GPT-4o API using the `openai.Completion.create()` function.
2. The prompt passed to the API includes the user's input, and the model is instructed to generate 5 prompt suggestions based on that input.
3. The function returns a list of the generated prompt suggestions.
4. In the `main()` function, the user is prompted to enter a query. The `get_prompt_suggestions()` function is called with the user's input, and the resulting suggestions are printed to the console.
5. The user can continue entering queries until they enter 'q' to quit the application.

To use this application, you'll need to replace `"YOUR_API_KEY"` with your actual OpenAI API key. You can obtain an API key by signing up for an OpenAI account and creating a new API key on the API Keys page.

This example demonstrates how you can leverage the GPT-4o API to provide users with prompt suggestions based on their input. The model's ability to understand and generate relevant prompts can be very useful in applications where users need help formulating effective queries or prompts.

You can further enhance this application by adding features like auto-complete, displaying the generated prompts in a user-friendly interface, or allowing users to select and use the suggested prompts directly.

Citations:
[1] https://github.com/TheStoneMX/conversation_with_GPT4o
[2] https://www.reddit.com/r/pythontips/comments/1d6ksjq/i_reverse_engineered_the_gpt4o_voice_assistant/
[3] https://tilburg.ai/2024/05/tutorial-gpt-4o-api/
[4] https://www.datacamp.com/tutorial/gpt4o-api-openai-tutorial
[5] https://ploomber.io/blog/gpt-4o-flask/