app.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import streamlit as st
  2. import asyncio
  3. from autogen import AssistantAgent, UserProxyAgent
  4. st.write("""# AutoGen Chat Agents""")
  5. class TrackableAssistantAgent(AssistantAgent):
  6. def _process_received_message(self, message, sender, silent):
  7. with st.chat_message(sender.name):
  8. st.markdown(message)
  9. return super()._process_received_message(message, sender, silent)
  10. class TrackableUserProxyAgent(UserProxyAgent):
  11. def _process_received_message(self, message, sender, silent):
  12. with st.chat_message(sender.name):
  13. st.markdown(message)
  14. return super()._process_received_message(message, sender, silent)
  15. selected_model = None
  16. selected_key = None
  17. with st.sidebar:
  18. st.header("OpenAI Configuration")
  19. selected_model = st.selectbox("Model", ['gpt-3.5-turbo', 'gpt-4'], index=1)
  20. selected_key = st.text_input("API Key", type="password")
  21. with st.container():
  22. # for message in st.session_state["messages"]:
  23. # st.markdown(message)
  24. user_input = st.chat_input("Type something...")
  25. if user_input:
  26. if not selected_key or not selected_model:
  27. st.warning(
  28. 'You must provide valid OpenAI API key and choose preferred model', icon="⚠️")
  29. st.stop()
  30. llm_config = {
  31. "request_timeout": 600,
  32. "config_list": [
  33. {
  34. "model": selected_model,
  35. "api_key": selected_key
  36. }
  37. ]
  38. }
  39. # create an AssistantAgent instance named "assistant"
  40. assistant = TrackableAssistantAgent(
  41. name="assistant", llm_config=llm_config)
  42. # create a UserProxyAgent instance named "user"
  43. user_proxy = TrackableUserProxyAgent(
  44. name="user", human_input_mode="NEVER", llm_config=llm_config)
  45. # Create an event loop
  46. loop = asyncio.new_event_loop()
  47. asyncio.set_event_loop(loop)
  48. # Define an asynchronous function
  49. async def initiate_chat():
  50. await user_proxy.a_initiate_chat(
  51. assistant,
  52. message=user_input,
  53. )
  54. # Run the asynchronous function within the event loop
  55. loop.run_until_complete(initiate_chat())