import streamlit as st import openai import os from openai import OpenAI # API Key Input api_key = os.environ['OPENAI_API_KEY'] client = OpenAI( api_key=api_key ) # Set up the OpenAI API client openai.api_key = api_key def generate_ai_response(prompt): """Simulates generating a response from an AI model. Args: prompt: The prompt to send to the AI model. Returns: response from the AI model. """ try: completion = openai.chat.completions.create( model="gpt-4o", messages=[ { "role": "developer", "content": [ { "type": "text", "text": """ You are a programming assistant focused on providing accurate, clear, and concise answers to technical questions. Your goal is to help users solve programming problems efficiently, explain concepts clearly, and provide examples when appropriate. Use a professional yet approachable tone. """ } ] }, { "role": "user", "content": [ { "type": "text", "text": prompt } ] } ] ) # Extract and display the response model_response = completion.choices[0].message.content st.success("Here's the response:") return model_response except Exception as e: st.error(f"An error occurred: {e}") return None # Define common programming tasks programming_tasks = [ "Write a function to reverse a string", "Create a class to represent a bank account", "Implement a binary search algorithm", "Write a script to scrape data from a website", "Create a function to validate an email address", "Implement a linked list data structure", "Write a program to find the factorial of a number", "Create a function to sort a list of numbers", "Implement a queue data structure", "Write a program to convert Celsius to Fahrenheit", "Create a recursive function to calculate Fibonacci numbers", "Write a function to check if a string is a palindrome", "Implement a stack data structure" ] # Streamlit app st.title("AI Programming Task Assistant") # Task selection selected_task = st.selectbox("Select a programming task:", programming_tasks) # Task details input task_details = st.text_area("Provide details about the task:", height=150) # Generate response button if st.button("Get Response"): if not task_details: st.warning("Please provide details about the task.") else: # Construct the prompt prompt = f"Programming Task: {selected_task}\nDetails: {task_details}" if not api_key: st.error("Please provide your OpenAI API Key.") else: with st.spinner("Thinking..."): # Simulate AI model response response = generate_ai_response(prompt) st.write(response)