Skip to content

Including Conversation History in a Gradio Chat Function #1338

@pietheinstrengholt

Description

@pietheinstrengholt

I have written this function to use the OpenAI Agent SDK along with Gradio. I would like to pass in the history. I learned that you can use the result.to_input_list() function to obtain an input list that concatenates the original input you provided. However, how does this work with the code below?

async def chat(message, history):
    try:
        # Make an agent with name, instructions, model
        agent = Agent(
            name="ADR Writer", 
            instructions=system_prompt,
            model=OpenAIChatCompletionsModel(
                model="gpt-4o-mini",
                openai_client=openai_client
            ),
            tools=tools
        )
        result = await Runner.run(agent, message)
        return result.final_output
    except OpenAIError as e:
        print(f"OpenAI API Error: {str(e)}")
    except Exception as e:
        print(f"An unexpected error occurred: {str(e)}")

gr.ChatInterface(fn=chat, type="messages").launch()

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion about using the SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions