paint-brush
Ki jan yo bati workflows AI nan mond reyèl la ak AutoGen: Gid etap pa etappa@yourtechbud
1,254 lekti
1,254 lekti

Ki jan yo bati workflows AI nan mond reyèl la ak AutoGen: Gid etap pa etap

pa YourTechBud21m2024/09/08
Read on Terminal Reader

Twò lontan; Pou li

Entegre Jenerative AI nan aplikasyon yo se enteresan, men li difisil. Chèn rapid senp yo souvan tonbe kout, kidonk bati workflows ajan ak AutoGen esansyèl. Flux travay sa yo fè AI plis detèminist ak serye, ede ou pran konpetans AI ou nan pwochen nivo.
featured image - Ki jan yo bati workflows AI nan mond reyèl la ak AutoGen: Gid etap pa etap
YourTechBud HackerNoon profile picture
0-item

Kidonk, ou te jwe ak gwo modèl lang epi w ap kòmanse entegre Jenerative AI nan aplikasyon w yo? Sa fantastik! Men, an nou reyèl. LLM yo pa toujou konpòte yo jan nou vle yo. Yo tankou timoun mechan ki gen tèt pa yo!


Ou byento reyalize ke chenn rapid senp yo jis pa ase. Pafwa, nou bezwen yon bagay plis. Pafwa, nou bezwen workflows milti-ajan! Se la kote AutoGen antre.


Ann pran yon egzanp. Imajine w ap fè yon aplikasyon pou pran nòt (Klèman, mond lan pa gen ase yo. 😝). Men, hey, nou vle fè yon bagay espesyal. Nou vle pran nòt ki senp, anvan tout koreksyon ke yon itilizatè ba nou epi tounen l 'nan yon dokiman konplètman restriktire, ki ranpli ak yon rezime, yon tit si ou vle, ak yon lis otomatik pou fè nan travay. Epi nou vle fè tout bagay sa yo san yo pa kraze yon swe - byen, pou ajan AI ou yo omwen.


Oke, kounye a, mwen konnen sa w ap panse - "Èske sa yo pa tankou pwogram rookie?" Pou sa, mwen di, ou gen rezon. Vle di... men dwat! Men, pa dwe twonpe pa senplisite workflow la. Konpetans ou pral aprann isit la - tankou manyen ajan AI, mete ann aplikasyon kontwòl workflow, ak jere istwa konvèsasyon - pral ede w mennen jwèt AI ou a nan pwochen nivo.


Se konsa, boukle, paske nou pral aprann ki jan fè workflows AI lè l sèvi avèk AutoGen!



Anvan nou kòmanse, sonje ke ou ka jwenn yon lyen nan tout kòd sous la sou GitHub .

Bati Workflows Custom

Ann kòmanse ak premye ka itilize a - "Jenerasyon yon rezime pou nòt la ki te swiv pa yon tit kondisyonèl". Pou nou jis, nou pa reyèlman bezwen sèvi ak ajan isit la. Men, hey, nou te kòmanse yon kote dwa?

Etap 1: Kreye konfigirasyon baz LLM nou an

Kad ajantik tankou AutoGen toujou mande pou nou konfigirasyon paramèt modèl yo. Nou ap pale de modèl la ak modèl repli pou itilize, tanperati a, e menm anviwònman tankou timeout ak kachèt. Nan ka AutoGen, anviwònman sa a sanble yon bagay tankou sa a:


 # build the gpt_configuration object base_llm_config = { "config_list": [ { "model": "Llama-3-8B-Instruct", "api_key": os.getenv("OPENAI_API_KEY"), "base_url": os.getenv("OPENAI_API_URL"), } ], "temperature": 0.0, "cache_seed": None, "timeout": 600, }


Kòm ou ka wè, mwen se yon gwo fanboy AI louvri-sous ak sèmante pa Llama 3. Ou ka fè AutoGen lonje dwèt sou nenpòt sèvè OpenAI-konpatib enferans pa senpleman konfigirasyon yo api_key ak base_url valè. Donk, santi yo lib pou itilize Groq, Together.ai, oswa menm vLLM pou òganize modèl ou lokalman. Mwen itilize Inferix .


Li vrèman fasil konsa!


Mwen kirye! Èske ou ta enterese nan yon gid menm jan an pou sous louvri AI hosting? Kite m 'konnen nan kòmantè yo.

Etap 2: Kreye ajan nou yo

Inisyalize ajan konvèsasyon nan AutoGen se trè senp; tou senpleman bay konfigirasyon LLM baz la ansanm ak yon mesaj sistèm epi ou bon pou ale.


 import autogen def get_note_summarizer(base_llm_config: dict): # A system message to define the role and job of our agent system_message = """You are a helpful AI assistant. The user will provide you a note. Generate a summary describing what the note is about. The summary must follow the provided "RULES". "RULES": - The summary should be not more than 3 short sentences. - Don't use bullet points. - The summary should be short and concise. - Identify and retain any "catchy" or memorable phrases from the original text - Identify and correct all grammatical errors. - Output the summary and nothing else.""" # Create and return our assistant agent return autogen.AssistantAgent( name="Note_Summarizer", # Lets give our agent a nice name llm_config=base_llm_config, # This is where we pass the llm configuration system_message=system_message, ) def get_title_generator(base_llm_config: dict): # A system message to define the role and job of our agent system_message = """You are a helpful AI assistant. The user will provide you a note along with a summary. Generate a title based on the user's input. The title must be witty and easy to read. The title should accurate present what the note is about. The title must strictly be less than 10 words. Make sure you keep the title short. Make sure you print the title and nothing else. """ # Create and return our assistant agent return autogen.AssistantAgent( name="Title_Generator", llm_config=base_llm_config, system_message=system_message, )


Pati ki pi enpòtan nan kreye ajan se system_message la. Pran yon ti moman pou gade system_message mwen te itilize pou configure ajan mwen yo.


Li enpòtan pou sonje fason Ajan AI nan AutoGen travay se lè yo patisipe nan yon konvèsasyon . Fason yo entèprete ak pote konvèsasyon an pi devan konplètman depann de system_message yo konfigirasyon avèk yo. Sa a se youn nan kote ou pral pase kèk tan pou fè bagay yo dwat.


Nou bezwen jis yon lòt ajan. Yon ajan pou aji kòm yon prokurasyon pou nou moun. Yon ajan ki ka kòmanse konvèsasyon an ak "nòt la" kòm premye èd li.


 def get_user(): # A system message to define the role and job of our agent system_message = "A human admin. Supplies the initial prompt and nothing else." # Create and return our user agent return autogen.UserProxyAgent( name="Admin", system_message=system_message, human_input_mode="NEVER", # We don't want interrupts for human-in-loop scenarios code_execution_config=False, # We definitely don't want AI executing code. default_auto_reply=None, )


Pa gen anyen anpenpan k ap pase isit la. Jis sonje ke mwen te mete paramèt default_auto_reply nan None . Sa enpòtan. Mete sa a nan okenn asire w ke konvèsasyon an fini chak fwa yo voye ajan itilizatè a yon mesaj.


Oops, mwen totalman bliye kreye ajan sa yo. Ann fè sa byen vit.


 # Create our agents user = get_user() note_summarizer = get_note_summarizer(base_llm_config) title_generator = get_title_generator(base_llm_config)

Etap 3: Enstalasyon Kowòdinasyon Ajan Sèvi ak yon GroupChat

Dènye pyès devinèt la ap fè ajan nou yo kowòdone. Nou bezwen detèmine sekans patisipasyon yo epi deside ki ajan yo ta dwe.


Oke, sa se plis pase yon sèl pyès. Men, ou jwenn pwen an! 🙈


Yon solisyon posib ta dwe kite AI konnen sekans kote Ajan yo patisipe. Sa a se pa yon move lide. An reyalite, sa a se opsyon ale-a mwen lè w ap fè fas ak pwoblèm konplèks kote nati workflow la dinamik.


Sepandan, apwòch sa a gen dezavantaj li yo. Reyalite frape ankò! Ajan ki responsab pou pran desizyon sa yo souvan bezwen yon gwo modèl, sa ki lakòz pi gwo latans ak pri. Anplis de sa, gen yon risk ke li ta ka pran desizyon ki pa kòrèk.


Pou workflows detèminist, kote nou konnen sekans etap yo davans, mwen renmen pwan ren yo ak dirije bato a tèt mwen. Erezman, AutoGen sipòte ka itilize sa a ak yon karakteristik pratik ki rele GroupChat .


 from autogen import GroupChatManager from autogen.agentchat.groupchat import GroupChat from autogen.agentchat.agent import Agent def get_group_chat(agents, generate_title: bool = False): # Define the function which decides the agent selection order def speaker_selection_method(last_speaker: Agent, group_chat: GroupChat): # The admin will always forward the note to the summarizer if last_speaker.name == "Admin": return group_chat.agent_by_name("Note_Summarizer") # Forward the note to the title generator if the user wants a title if last_speaker.name == "Note_Summarizer" and generate_title: return group_chat.agent_by_name("Title_Generator") # Handle the default case - exit return None return GroupChat( agents=agents, messages=[], max_round=3, # There will only be 3 turns in this group chat. The group chat will exit automatically post that. speaker_selection_method=speaker_selection_method, )


Imajine yon GroupChat kòm yon gwoup WhatsApp kote tout ajan yo ka koze ak kolabore. Konfigirasyon sa a pèmèt ajan yo bati sou travay youn lòt. Klas GroupChat la ansanm ak yon klas konpayon ki rele GroupChatManager aji tankou admin gwoup yo, kenbe tras de tout mesaj chak ajan voye pou asire tout moun rete nan bouk la ak istwa konvèsasyon an.


Nan snippet kòd ki anwo a, nou te kreye yon GroupChat ak yon speaker_selection_method koutim. speaker_selection_method pèmèt nou presize workflow koutim nou an. Isit la nan yon reprezantasyon vizyèl nan menm bagay la.


Workflow pou jenere yon rezime nòt ansanm ak yon tit si ou vle


Depi speaker_selection_method la se esansyèlman yon fonksyon Python, nou ka fè tou sa nou vle ak li! Sa a ede nou kreye kèk workflows vrèman pwisan. Pou egzanp, nou ta ka:

  • Pè ajan pou verifye travay youn lòt.
  • Enplike yon ajan "Depanaj" si nenpòt nan ajan anvan yo bay yon erè.
  • Deklanche webhooks nan sistèm ekstèn pou enfòme yo sou pwogrè yo te fè.


Imajine posiblite yo! 😜

Etap 4: Mete tout bagay epi kòmanse konvèsasyon an

Dènye etap la se kreye yon egzanp nan GroupChat la, vlope li andedan yon GroupChatManager ak kòmanse konvèsasyon an.


 # Create our group chat groupchat = get_group_chat([user, note_summarizer, title_generator], generate_title=True) manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=base_llm_config) # Start the chat user.initiate_chat( manager, clear_history=True, message=note, )


Remak: itilizatè a ap diskite ak GroupChatManager la , pa ajan endividyèl yo. Li pa gen okenn siy ki ajan ki pral rantre nan konvèsasyon an pou bay repons final la. Sournwa, dwa?


Pwodiksyon an pral gade yon bagay tankou sa a:


 Admin (to chat_manager): Note: Convo with editor: - discuss titles and thumbnails - discuss video editing tips tracker - Zeeshan presents the tracker - what trick helps with what - he decidedls if we can experiment with something new - make sure all all videos since how config management works in k8s are backed u - make list of YouTube thumbnail templates - make list of YouTube idea generation limits -------------------------------------------------------------------------------- Next speaker: Note_Summarizer Note_Summarizer (to chat_manager): The note is about a conversation with an editor regarding video production. They discussed titles and thumbnails, as well as a video editing tips tracker presented by Zeeshan, which highlights tricks for specific tasks. Additionally, they ensured that all videos on Kubernetes configuration management are backed up and created lists of YouTube thumbnail templates and idea generation limits. -------------------------------------------------------------------------------- Next speaker: Title_Generator Title_Generator (to chat_manager): "Video Production Chat: Titles, Thumbnails, and Editing Tips" --------------------------------------------------------------------------------

Pran Kontwòl Konvèsasyon an

Apre sa, nou pral plonje nan ka itilizasyon final la: pran yon "nòt" bay, restriktire li pou pi bon klè, ak Lè sa a, kreye yon lis travay pou itilizatè a.


Men ki jan nou pral ale sou li:


Workflow pou parafrase yon nòt ak jenere travay nan li


Nou pral kòmanse pa idantifye yon lis sijè ki kouvri nan nòt la. Lis sa a se fòs ki mennen nan tout pwosesis la. Li etabli seksyon yo pou nòt refòme a epi li detèmine nivo detay pou travay nou yo pwodwi.


Gen yon sèl ti pwoblèm. Ajan Paraphrazer ak Task_Creator pa vrèman sousye de pwodiksyon youn lòt. Yo sèlman pran swen sou pwodiksyon Topic_Analyzer la.


Se konsa, nou bezwen yon fason pou kenbe repons ajan sa yo soti nan ankonbre istwa konvèsasyon an, oswa li pral dezòd konplè. Nou te deja pran kontwòl workflow la; kounye a, li lè yo dwe bòs nan istwa a konvèsasyon tou! 😎

Etap 1: Kreye ajan yo

Premye bagay an premye, nou bezwen mete ajan nou yo. Mwen pa pral anwiye ou ak detay yo, kidonk isit la se kòd la:


 def get_topic_analyzer(base_llm_config: dict): # A system message to define the role and job of our agent system_message = """You are a helpful AI assistant. The user will provide you a note. Generate a list of topics discussed in that note. The output must obey the following "RULES": "RULES": - Output should only contain the important topics from the note. - There must be atleast one topic in output. - Don't reuse the same text from user's note. - Don't have more than 10 topics in output.""" # Create and return our assistant agent return autogen.AssistantAgent( name="Topic_Analyzer", llm_config=base_llm_config, system_message=system_message, ) def get_paraphrazer(base_llm_config: dict): # A system message to define the role and job of our agent system_message = """You are a helpful AI content editor. The user will provide you a note along with a summary. Rewrite that note and make sure you cover everything in the note. Do not include the title. The output must obey the following "RULES": "RULES": - Output must be in markdown. - Make sure you use each points provided in summary as headers. - Each header must start with `##`. - Headers are not bullet points. - Each header can optionally have a list of bullet points. Don't put bullet points if the header has no content. - Strictly use "-" to start bullet points. - Optionally make an additional header named "Addional Info" to cover points not included in the summary. Use "Addional Info" header for unclassified points. - Identify and correct spelling & grammatical mistakes.""" # Create and return our assistant agent return autogen.AssistantAgent( name="Paraphrazer", llm_config=base_llm_config, system_message=system_message, ) def get_tasks_creator(base_llm_config: dict): # A system message to define the role and job of our agent system_message = """You are a helpful AI personal assistant. The user will provide you a note along with a summary. Identify each task the user has to do as next steps. Make sure to cover all the action items mentioned in the note. The output must obey the following "RULES": "RULES": - Output must be an YAML object with a field named tasks. - Make sure each task object contains fields title and description. - Extract the title based on the tasks the user has to do as next steps. - Description will be in markdown format. Feel free to include additional formatting and numbered lists. - Strictly use "-" or "dashes" to start bullet points in the description field. - Output empty tasks array if no tasks were found. - Identify and correct spelling & grammatical mistakes. - Identify and fix any errors in the YAML object. - Output should strictly be in YAML with no ``` or any additional text.""" # Create and return our assistant agent return autogen.AssistantAgent( name="Task_Creator", llm_config=base_llm_config, system_message=system_message, )

Etap 2: Kreye yon GroupChat Custom

Malerezman. AutoGen pa pèmèt nou kontwole istwa konvèsasyon an dirèkteman. Se konsa, nou bezwen ale pi devan epi pwolonje klas GroupChat la ak aplikasyon koutim nou an.


 class CustomGroupChat(GroupChat): def __init__(self, agents): super().__init__(agents, messages=[], max_round=4) # This function get's invoked whenever we want to append a message to the conversation history. def append(self, message: Dict, speaker: Agent): # We want to skip messages from the Paraphrazer and the Task_Creator if speaker.name != "Paraphrazer" and speaker.name != "Task_Creator": super().append(message, speaker) # The `speaker_selection_method` now becomes a function we will override from the base class def select_speaker(self, last_speaker: Agent, selector: AssistantAgent): if last_speaker.name == "Admin": return self.agent_by_name("Topic_Analyzer") if last_speaker.name == "Topic_Analyzer": return self.agent_by_name("Paraphrazer") if last_speaker.name == "Paraphrazer": return self.agent_by_name("Task_Creator") # Return the user agent by default return self.agent_by_name("Admin")


Nou pase sou plas de fonksyon ki soti nan klas GroupChat de baz la:

  • append - Sa a kontwole ki mesaj yo ajoute nan istwa konvèsasyon an.
  • select_speaker - Sa a se yon lòt fason pou presize speaker_selection_method la.


Men, tann, sou plonje pi fon nan kòd AutoGen a, mwen reyalize ke GroupChatManager la fè chak ajan kenbe istwa a konvèsasyon tou. Pa mande m poukisa. Mwen vrèman pa konnen!


Se konsa, ann pwolonje GroupChatManager la tou pou ranje sa:


 class CustomGroupChatManager(GroupChatManager): def __init__(self, groupchat, llm_config): super().__init__(groupchat=groupchat, llm_config=llm_config) # Don't forget to register your reply functions self.register_reply(Agent, CustomGroupChatManager.run_chat, config=groupchat, reset_config=GroupChat.reset) def run_chat( self, messages: Optional[List[Dict]] = None, sender: Optional[Agent] = None, config: Optional[GroupChat] = None, ) -> Union[str, Dict, None]: """Run a group chat.""" if messages is None: messages = self._oai_messages[sender] message = messages[-1] speaker = sender groupchat = config for i in range(groupchat.max_round): # set the name to speaker's name if the role is not function if message["role"] != "function": message["name"] = speaker.name groupchat.append(message, speaker) if self._is_termination_msg(message): # The conversation is over break # We do not want each agent to maintain their own conversation history history # broadcast the message to all agents except the speaker # for agent in groupchat.agents: # if agent != speaker: # self.send(message, agent, request_reply=False, silent=True) # Pro Tip: Feel free to "send" messages to the user agent if you want to access the messages outside of autogen for agent in groupchat.agents: if agent.name == "Admin": self.send(message, agent, request_reply=False, silent=True) if i == groupchat.max_round - 1: # the last round break try: # select the next speaker speaker = groupchat.select_speaker(speaker, self) # let the speaker speak # We'll now have to pass their entire conversation of messages on generate_reply # Commented OG code: reply = speaker.generate_reply(sender=self) reply = speaker.generate_reply(sender=self, messages=groupchat.messages) except KeyboardInterrupt: # let the admin agent speak if interrupted if groupchat.admin_name in groupchat.agent_names: # admin agent is one of the participants speaker = groupchat.agent_by_name(groupchat.admin_name) # We'll now have to pass their entire conversation of messages on generate_reply # Commented OG code: reply = speaker.generate_reply(sender=self) reply = speaker.generate_reply(sender=self, messages=groupchat.messages) else: # admin agent is not found in the participants raise if reply is None: break # The speaker sends the message without requesting a reply speaker.send(reply, self, request_reply=False) message = self.last_message(speaker) return True, None


Mwen te fè kèk ti chanjman nan aplikasyon orijinal la. Ou ta dwe kapab swiv kòmantè yo konnen plis.


Men, gen yon bagay mwen reyèlman vle mete aksan sou. Ou ka pase sou plas "run_chat" metòd GroupChatManager la pou plugin nan pwòp motè workflow ou tankou Apache Airflow oswa Temporal. Pratikan nan sistèm distribiye konnen egzakteman ki jan pwisan kapasite sa a se!

Etap 3: Mete tout bagay epi kòmanse konvèsasyon an

Nou mete tout bagay sa yo tankou egzanp anvan an epi gade ti bebe sa a ronron! 🐱


 # Create our agents user = get_user() topic_analyzer = get_topic_analyzer(base_llm_config) paraphrazer = get_paraphrazer(base_llm_config) task_creator = get_tasks_creator(base_llm_config) # Create our group chat groupchat = CustomGroupChat(agents=[user, topic_analyzer, paraphrazer, task_creator]) manager = CustomGroupChatManager(groupchat=groupchat, llm_config=base_llm_config) # Start the chat user.initiate_chat( manager, clear_history=True, message=note, ) # Lets print the count of tasks just for fun chat_messages = user.chat_messages.get(manager) if chat_messages is not None: for message in chat_messages: if message.get("name") == "Task_Creator": taskList = yaml.safe_load(message.get("content")) # type: ignore l = len(taskList.get("tasks")) print(f"Got {l} tasks from Task_Creator.")


Pwodiksyon an pral gade yon bagay tankou sa a:


 Admin (to chat_manager): Note: Convo with editor: - discuss titles and thumbnails - discuss video editing tips tracker - Zeeshan presents the tracker - what trick helps with what - he decidedls if we can experiment with something new - make sure all all videos since how config management works in k8s are backed u - make list of YouTube thumbnail templates - make list of YouTube idea generation limits -------------------------------------------------------------------------------- Topic_Analyzer (to chat_manager): Here is the list of topics discussed in the note: 1. Titles 2. Thumbnails 3. Video editing tips 4. Config management in Kubernetes (k8s) 5. YouTube thumbnail templates 6. YouTube idea generation limits -------------------------------------------------------------------------------- Paraphrazer (to chat_manager): Here is the rewritten note in markdown format: ## Titles - Discuss titles and thumbnails with the editor ## Video Editing Tips Tracker ### Zeeshan presents the tracker - What trick helps with what - He decides if we can experiment with something new ## Config Management in Kubernetes (k8s) - Make sure all videos since how config management works in k8s are backed up ## YouTube Thumbnail Templates - Make a list of YouTube thumbnail templates ## YouTube Idea Generation Limits - Make a list of YouTube idea generation limits ## Additional Info - Discuss video editing tips tracker with Zeeshan - Present the tracker and decide if we can experiment with something new -------------------------------------------------------------------------------- Task_Creator (to chat_manager): tasks: - title: Discuss Titles and Thumbnails description: >- - Discuss titles and thumbnails with the editor This task involves having a conversation with the editor to discuss the titles and thumbnails for the videos. - title: Discuss Video Editing Tips Tracker description: >- - Zeeshan presents the tracker - Discuss what trick helps with what - Decide if we can experiment with something new This task involves discussing the video editing tips tracker presented by Zeeshan, understanding what tricks help with what, and deciding if it's possible to experiment with something new. - title: Back up All Videos Since How Config Management Works in k8s description: >- - Make sure all videos since how config management works in k8s are backed up This task involves ensuring that all videos related to config management in Kubernetes (k8s) are backed up. - title: Create List of YouTube Thumbnail Templates description: >- - Make list of YouTube thumbnail templates This task involves creating a list of YouTube thumbnail templates. - title: Create List of YouTube Idea Generation Limits description: >- - Make list of YouTube idea generation limits This task involves creating a list of YouTube idea generation limits. -------------------------------------------------------------------------------- Got 5 tasks from Task_Creator.


Wi. Byenvini nan laj la nan AI ban nou hoomans travay fè! (Ki kote tout bagay te ale mal? 🤷‍♂️)

Konklizyon

Bati aplikasyon jeneratif AI-kondwi difisil. Men, li ka fè ak bon zouti. Pou rezime:

  • Ajan AI yo déblotché kapasite etonan sa a nan modèl pwoblèm konplèks kòm konvèsasyon.
  • Si yo fè sa byen, li ka ede fè aplikasyon AI nou yo pi detèminist ak serye .
  • Zouti tankou AutoGen bay nou yon kad ki gen abstraksyon senp pou konstwi Ajan AI.


Kòm pwochen etap yo, ou ka tcheke resous sa yo pou plonje pi fon nan mond ajan AI: