A programming framework for agentic AI
adding streamlit sample app (#5306)
Hussein Koprly75968565a105
diff --git a/python/samples/agentchat_streamlit/.gitignore b/python/samples/agentchat_streamlit/.gitignorenew file mode 100644index 000000000..ec578428f--- /dev/null+++ b/python/samples/agentchat_streamlit/.gitignore@@ -0,0 +1 @@+model_config.yml\ No newline at end of filediff --git a/python/samples/agentchat_streamlit/README.md b/python/samples/agentchat_streamlit/README.mdnew file mode 100644index 000000000..abcc451c9--- /dev/null+++ b/python/samples/agentchat_streamlit/README.md@@ -0,0 +1,45 @@+# Streamlit AgentChat Sample Application++This is a sample AI chat assistant built with [Streamlit](https://streamlit.io/)++## Setup++Install the `streamlit` package with the following command:++```bash+pip install streamlit+```++To use Azure OpenAI models or models hosted on OpenAI-compatible API endpoints,+you need to install the `autogen-ext[openai,azure]` package. You can install it with the following command:++```bash+pip install "autogen-ext[openai,azure]"+# pip install "autogen-ext[openai]" for OpenAI models+```++Create a new file named `model_config.yml` in the the same directory as the script+to configure the model you want to use.++For example, to use `gpt-4o-mini` model from Azure OpenAI, you can use the following configuration:++```yml+provider: autogen_ext.models.openai.AzureOpenAIChatCompletionClient+config:+ azure_deployment: "gpt-4o-mini"+ model: gpt-4o-mini+ api_version: REPLACE_WITH_MODEL_API_VERSION+ azure_endpoint: REPLACE_WITH_MODEL_ENDPOINT+ api_key: REPLACE_WITH_MODEL_API_KEY+```++For more information on how to configure the model and use other providers,+please refer to the [Models documentation](https://microsoft.github.io/autogen/stable/user-guide/agentchat-user-guide/tutorial/models.html).++## Run++Run the following command to start the web application:++```bash+streamlit run main.py+```\ No newline at end of filediff --git a/python/samples/agentchat_streamlit/agent.py b/python/samples/agentchat_streamlit/agent.pynew file mode 100644index 000000000..5ce808333--- /dev/null+++ b/python/samples/agentchat_streamlit/agent.py@@ -0,0 +1,27 @@+import yaml++from autogen_core import CancellationToken+from autogen_agentchat.messages import TextMessage+from autogen_agentchat.agents import AssistantAgent+from autogen_ext.models.openai import AzureOpenAIChatCompletionClient+++class Agent:+ def __init__(self):+ # Load the model client from config.+ with open("model_config.yml", "r") as f:+ model_config = yaml.safe_load(f)+ model_client = AzureOpenAIChatCompletionClient.load_component(model_config)+ self.agent = AssistantAgent(+ name="assistant",+ model_client=model_client,+ system_message="You are a helpful AI assistant.",+ )++ async def chat(self, prompt: str) -> str:+ response = await self.agent.on_messages(+ [TextMessage(content=prompt, source="user")],+ CancellationToken(),+ )++ return response.chat_message.contentdiff --git a/python/samples/agentchat_streamlit/main.py b/python/samples/agentchat_streamlit/main.pynew file mode 100644index 000000000..326e9a650--- /dev/null+++ b/python/samples/agentchat_streamlit/main.py@@ -0,0 +1,37 @@+import asyncio+import streamlit as st+from agent import Agent+++def main():+ st.set_page_config(page_title="AI Chat Assistant", page_icon="🤖")+ st.title("AI Chat Assistant 🤖")++ # adding agent object to session state to persist across sessions+ # stramlit reruns the script on every user interaction+ if "agent" not in st.session_state:+ st.session_state.agent = Agent()++ # initialize chat history+ if "messages" not in st.session_state:+ st.session_state.messages = []++ # displying chat history messages+ for message in st.session_state.messages:+ with st.chat_message(message["role"]):+ st.markdown(message["content"])++ prompt = st.chat_input("Type a message...")+ if prompt:+ st.session_state.messages.append({"role": "user", "content": prompt})+ with st.chat_message("user"):+ st.markdown(prompt)++ response = asyncio.run(st.session_state.agent.chat(prompt))+ st.session_state.messages.append({"role": "assistant", "content": response})+ with st.chat_message("assistant"):+ st.markdown(response)+++if __name__ == "__main__":+ main()