#https://python.langchain.com/docs/how_to/configure/ import gradio as gr import os from langchain_openai import ChatOpenAI from langchain_core.prompts import PromptTemplate from langchain_core.runnables import ConfigurableField def process_with_config(topic, temperature, mode, api_key): try: # Set API key os.environ["OPENAI_API_KEY"] = api_key # Initialize configurable model with GPT-4o-mini model = ChatOpenAI( model="gpt-4o-mini", # Specifically using GPT-4o-mini openai_api_key=api_key, temperature=0 ).configurable_fields( temperature=ConfigurableField( id="llm_temperature", name="LLM Temperature", description="Temperature for GPT-4o-mini response generation" ) ) # Create configurable prompt prompt = PromptTemplate.from_template( "Tell me a {mode} about {topic}" ).configurable_alternatives( ConfigurableField(id="prompt"), default_key="joke", poem=PromptTemplate.from_template("Write a poem about {topic}"), joke=PromptTemplate.from_template("Tell me a joke about {topic}") ) # Create chain chain = prompt | model # Configure and run response = chain.with_config( configurable={ "llm_temperature": float(temperature), "prompt": mode } ).invoke({"topic": topic}) return response.content except Exception as e: return f"Error: {str(e)}" finally: # Clear API key from environment for security if "OPENAI_API_KEY" in os.environ: del os.environ["OPENAI_API_KEY"] # Create Gradio interface demo = gr.Interface( fn=process_with_config, inputs=[ gr.Textbox( label="Topic", placeholder="Enter a topic...", lines=1 ), gr.Slider( minimum=0, maximum=1, value=0.5, step=0.1, label="Temperature (GPT-4o-mini)" ), gr.Radio( choices=["joke", "poem"], label="Mode", value="joke" ), gr.Textbox( label="OpenAI API Key", placeholder="Enter your OpenAI API key", type="password" ) ], outputs=gr.Textbox( label="Generated Response", lines=5 ), title="🤖 GPT-4o-mini Configuration Demo", description="Generate content using GPT-4o-mini with configurable temperature and mode" ) # Launch the application if __name__ == "__main__": demo.launch( share=False, server_name="0.0.0.0", server_port=7860 )