-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds multiple choice message handling to MesopUI (#198)
* Multiple choice WIP(1) * Multiple choice WIP(2) * Multiple choice WIP(3) * test fix * tyest fixes * added all caps instructions for message termination * added all caps instructions for message termination --------- Co-authored-by: Davorin Rusevljan <davorin.rusevljan@gmail.com>
- Loading branch information
1 parent
8aaba85
commit f0aaa96
Showing
17 changed files
with
392 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
docs/docs/en/api/fastagency/ui/mesop/data_model/ConversationMessage.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
# 0.5 - API | ||
# 2 - Release | ||
# 3 - Contributing | ||
# 5 - Template Page | ||
# 10 - Default | ||
search: | ||
boost: 0.5 | ||
--- | ||
|
||
::: fastagency.ui.mesop.data_model.ConversationMessage |
11 changes: 11 additions & 0 deletions
11
docs/docs/en/api/fastagency/ui/mesop/message/consume_responses.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
# 0.5 - API | ||
# 2 - Release | ||
# 3 - Contributing | ||
# 5 - Template Page | ||
# 10 - Default | ||
search: | ||
boost: 0.5 | ||
--- | ||
|
||
::: fastagency.ui.mesop.message.consume_responses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
docs/docs_src/user_guide/custom_user_interactions/main_mesop.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import os | ||
from typing import Annotated, Any, Dict, Optional | ||
|
||
from autogen import register_function | ||
from autogen.agentchat import ConversableAgent | ||
|
||
from fastagency import FastAgency | ||
from fastagency import UI | ||
from fastagency.base import MultipleChoice, SystemMessage, TextInput | ||
from fastagency.ui.mesop import MesopUI | ||
from fastagency.runtime.autogen.base import AutoGenWorkflows | ||
|
||
llm_config = { | ||
"config_list": [ | ||
{ | ||
"model": "gpt-4o-mini", | ||
"api_key": os.getenv("OPENAI_API_KEY"), | ||
} | ||
], | ||
"temperature": 0.0, | ||
} | ||
|
||
wf = AutoGenWorkflows() | ||
|
||
|
||
@wf.register(name="exam_practice", description="Student and teacher chat") | ||
def exam_learning(ui: UI, initial_message: str, session_id: str) -> str: | ||
|
||
def is_termination_msg(msg: dict[str, Any]) -> bool: | ||
return msg["content"] is not None and "TERMINATE" in msg["content"] | ||
|
||
student_agent = ConversableAgent( | ||
name="Student_Agent", | ||
system_message="You are a student writing a practice test. Your task is as follows:\n" | ||
" 1) Retrieve exam questions by calling a function.\n" | ||
" 2) Write a draft of proposed answers and engage in dialogue with your tutor.\n" | ||
" 3) Once you are done with the dialogue, register the final answers by calling a function.\n" | ||
" 4) Retrieve the final grade by calling a function.\n" | ||
"Finally, terminate the chat by saying 'TERMINATE'.", | ||
llm_config=llm_config, | ||
human_input_mode="NEVER", | ||
is_termination_msg=is_termination_msg, | ||
) | ||
teacher_agent = ConversableAgent( | ||
name="Teacher_Agent", | ||
system_message="You are a teacher.", | ||
llm_config=llm_config, | ||
human_input_mode="NEVER", | ||
is_termination_msg=is_termination_msg, | ||
) | ||
|
||
def retrieve_exam_questions( | ||
message: Annotated[str, "Message for examiner"] | ||
) -> Optional[str]: | ||
try: | ||
msg = MultipleChoice( | ||
sender="student", | ||
recipient="teacher", | ||
prompt=message, | ||
choices=[ | ||
"1) Mona Lisa", | ||
"2) Innovations", | ||
"3) Florence at the time of Leonardo", | ||
"4) The Last Supper", | ||
"5) Vitruvian Man", | ||
], | ||
default="1) Mona Lisa" | ||
) | ||
return ui.process_message(msg) | ||
except Exception as e: | ||
return f"retrieve_exam_questions() FAILED! {e}" | ||
|
||
def write_final_answers(message: Annotated[str, "Message for examiner"]) -> str: | ||
try: | ||
msg = SystemMessage( | ||
sender="function call logger", | ||
recipient="system", | ||
message={ | ||
"operation": "storing final answers", | ||
"content": message, | ||
}, | ||
) | ||
ui.process_message(msg) | ||
return "Final answers stored." | ||
except Exception as e: | ||
return f"write_final_answers() FAILED! {e}" | ||
|
||
def get_final_grade( | ||
message: Annotated[str, "Message for examiner"] | ||
) -> Optional[str]: | ||
try: | ||
msg = MultipleChoice( | ||
sender="student", | ||
recipient="teacher", | ||
prompt=message, | ||
choices=["A", "B", "C", "D", "F"], | ||
) | ||
return ui.process_message(msg) | ||
except Exception as e: | ||
return f"get_final_grade() FAILED! {e}" | ||
|
||
register_function( | ||
retrieve_exam_questions, | ||
caller=student_agent, | ||
executor=teacher_agent, | ||
name="retrieve_exam_questions", | ||
description="Get exam questions from examiner", | ||
) | ||
|
||
register_function( | ||
write_final_answers, | ||
caller=student_agent, | ||
executor=teacher_agent, | ||
name="write_final_answers", | ||
description="Write a final answers to exam questions to examiner, but only after discussing with the tutor first.", | ||
) | ||
|
||
register_function( | ||
get_final_grade, | ||
caller=student_agent, | ||
executor=teacher_agent, | ||
name="get_final_grade", | ||
description="Get the final grade after submitting the answers.", | ||
) | ||
|
||
chat_result = teacher_agent.initiate_chat( | ||
student_agent, | ||
message=initial_message, | ||
summary_method="reflection_with_llm", | ||
max_turns=10, | ||
) | ||
|
||
return chat_result.summary # type: ignore[no-any-return] | ||
|
||
|
||
app = FastAgency(wf=wf, ui=MesopUI()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
|
||
from autogen import UserProxyAgent | ||
from autogen.agentchat import ConversableAgent | ||
|
||
from fastagency import FastAgency | ||
from fastagency import UI | ||
from fastagency.ui.mesop import MesopUI | ||
from fastagency.runtime.autogen.base import AutoGenWorkflows | ||
|
||
from fastagency.api.openapi import OpenAPI | ||
|
||
|
||
llm_config = { | ||
"config_list": [ | ||
{ | ||
"model": "gpt-4o-mini", | ||
"api_key": os.getenv("OPENAI_API_KEY"), | ||
} | ||
], | ||
"temperature": 0.0, | ||
} | ||
|
||
WEATHER_OPENAPI_URL = "https://weather.tools.fastagency.ai/openapi.json" | ||
|
||
wf = AutoGenWorkflows() | ||
|
||
|
||
@wf.register(name="simple_weather", description="Weather chat") | ||
def weather_workflow(ui: UI, initial_message: str, session_id: str) -> str: | ||
|
||
weather_api = OpenAPI.create(openapi_url=WEATHER_OPENAPI_URL) | ||
|
||
user_agent = UserProxyAgent( | ||
name="User_Agent", | ||
system_message="You are a user agent", | ||
llm_config=llm_config, | ||
human_input_mode="NEVER", | ||
) | ||
weather_agent = ConversableAgent( | ||
name="Weather_Agent", | ||
system_message="You are a weather agent", | ||
llm_config=llm_config, | ||
human_input_mode="NEVER", | ||
) | ||
|
||
weather_api.register_for_llm(weather_agent) | ||
weather_api.register_for_execution(user_agent) | ||
|
||
chat_result = user_agent.initiate_chat( | ||
weather_agent, | ||
message=initial_message, | ||
summary_method="reflection_with_llm", | ||
max_turns=3, | ||
) | ||
|
||
return chat_result.summary # type: ignore[no-any-return] | ||
|
||
|
||
app = FastAgency(wf=wf, ui=MesopUI()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.