ทีมงานคุณภาพด้วย CrewAI

Max, Who's done well on his jobs
TakoDigital
Published in
4 min readMay 24, 2024

ปกติแล้วเวลาเราค้นหาหรือถามข้อมูลผ่าน LLM ของผู้ให้บริการต่างๆ เช่น ChatGPT, Ollama หรือ Gemini เป็นต้น เราก็จะถามทีละคำถามแล้วได้คำตอบมา บางทีเราก็ต้องพิมพ์คำถามและให้ข้อมูลที่ครอบคลุมเพื่อที่จะได้คำตอบที่ตรงตามที่เราต้องการ ซึ่ง CrewAI สามารถเข้ามาช่วยเราตรงนี้ได้ระดับหนึ่ง บวกกับ Prompt ที่เรากำหนดให้ด้วย และสามารถกำหนดความต้องการของผลลัพธ์ของเราได้ว่าให้สรุปข้อมูลที่เราค้นหาเป็นบทความ, วางแผนการเดินทาง, หรือแม้กระทั่งเขียน Code ตาม requirement ที่เราต้องการได้ ผ่าน Agents ที่กำหนดหน้าที่ให้ตามข้อมูลที่เราให้ไป

ในบทความนี้ก็จะเป็นการแนะนำวิธีการใช้งานเบื้องต้นของ CrewAI โดยที่นี้จะสร้างทีมเพื่อที่จะเขียนสรุปเกี่ยวกับ Framework ที่อยู่ใน Github ว่ามี functions อะไรบ้างรวมไปถึงแสดงตัวอย่างวิธีการใช้งาน function

ก่อนเริ่มสร้างทีมงานคุณภาพผ่าน CrewAI มาเตรียมความพร้อมกันก่อน

ในบทความนี้เราจะใช้ Local Ollama เป็นเครื่องมือให้กับลูกทีมของเรา โดยที่ใช้จะใช้ Model ที่ชื่อว่า llama2 parameters 7B (ที่ใช้ตัวนี้ไม่ใช่อะไรหรอก RAM มีแค่ 16Gb และตัว llama2 ใช้ไป 8Gb แบบจริงจัง) เผื่อใครที่ทำตามแล้วคอมมี RAM เยอะๆ ก็สามารถเลือกใช้ model ที่มี parameters ที่สูงกว่านี้ก็ได้ ตามข้อมูล spec requirements ด้านล่างนี้เลย

Model Llama2 Memory requirements

  • 7b models generally require at least 8GB of RAM
  • 13b models generally require at least 16GB of RAM
  • 70b models generally require at least 64GB of RAM

หรือถ้าใครมีคอมไม่แรงแต่อยากให้ประมวลผลเร็วๆ ก็สามารถใช้ OpenAI GPT-4 ได้ แต่เสียเงินนะ

วิธีการติดตั้ง Local Ollama

สามารถทำตาม Link นี้ได้เลย หลังจากที่ติดตั้ง Ollama เรียบร้อยแล้ว จะยังทำอะไรไม่ได้เพราะว่ามันเป็นแค่ตัวรัน Models เท่านั้น เราต้องโหลด Models เพื่อเอามารันในเครื่องของเราอีกที โดยใช้คำสั่ง

ollama pull llama2

หลังจากที่โหลด Models มาเรียบร้อยแล้ว ก็เปิด Terminals แล้วรันคำสั่ง ollama ls จะแสดง list ของ Models ที่เราได้โหลดลงมาไว้ในเครื่องของเราทั้งหมด วิธีใช้ก็สามารถพิมพ์คำสั่ง ollama run แล้วตามด้วยชื่อ Model ที่โหลดไว้เครื่องแล้ว

ollama run llama2

ลองถามดูก็จะได้ผลลัพธ์ประมาณนี้ … ไปที่ CrewAI กันต่อ

ก่อนสร้างทีมงานจาก CrewAI เรามาทำความรู้จัก functions หลักกันก่อนว่าหลักๆ มี function ตามด้านล่างนี้

Agent()

An agent as a member of a team, with specific skills and a particular job to do. Agents can have different roles like Researcher, Writer, or Customer Support, each contributing to the overall goal of the crew.

Task()

Tasks are individual assignments that agents complete. They encapsulate necessary information for execution, including a description, assigned agent, required tools, offering flexibility for various action complexities.

Tools()

A tool in CrewAI is a skill or function that agents can utilize to perform various actions. This includes tools from the crewAI Toolkit and LangChain Tools, enabling everything from simple searches to complex interactions and effective teamwork among agents.

Crew()

A crew in crewAI represents a collaborative group of agents working together to achieve a set of tasks. Each crew defines the strategy for task execution, agent collaboration, and the overall workflow.

มาสร้างทีมงานกัน

อันดับแรกสร้าง Agent หรือก็คือลูกทีมของเรานั้นเอง

from textwrap import dedent
from langchain_community.llms.ollama import Ollama

ollama = Ollama(model="llama2")

def frameworkReseacher(self):
return Agent(
role="Framework Research",
goal="Analyst the framework and provided description to extract",
backstory=dedent(f"""
Analyzing the framework and identifying key feature of the framework from various sources
"""),
tools=[],
llm=ollama,
verbose=True,
allow_delegation=True
)

โดยที่เรากำหนดหน้าที่ให้กับ Agent ว่าต้องการให้เราทำหน้าที่อะไรตามภาพด้านบน

  • role ก็คือหน้าที่ของ Agent
  • goal เป้าหมายที่เราต้องการให้ Agent ทำ
  • backstory เป็นประวัติว่าเคยทำอะไรมาก่อน แล้วก็เก่งในด้านไหน

หลังจากที่มีลูกทีมแล้ว ลูกทีมเราจะใช้อะไรทำงานล่ะ? ก็ต้องสร้าง Tools หรือเครื่องมือให้กับลูกทีมเราว่า แต่ละคน(Agent) จะใช้เครื่องมืออะไรบ้างในการหาข้อมูลและทำงานในงานที่ได้รับมอบหมาย

from crewai_tools import GithubSearchTool

githubFrameworkSearchTool = GithubSearchTool(
github_repo='https://github.com/tiangolo/fastapi',
content_types=['code'],
gh_token="TOKEN"
)

def frameworkReseacher(self):
return Agent(
role="Framework Research",
goal="Analyst the framework and provided description to extract",
backstory=dedent(f"""
Analyzing the framework and identifying key feature of the framework from various sources
"""),
tools=[
githubFrameworkSearchTool
],
llm=ollama,
verbose=True,
allow_delegation=True
)

Code ด้านบนเราก็จะใช้เป็น GithubSearchTool เป็นเครื่องมือที่ CrewAI มีมาให้อยู่แล้ว หลังจากที่สร้าง GithubSearchTool ก็เพิ่มเข้าไปใน Tools ของ Agent ได้เลย

หลังจากมีลูกทีมที่ดีและเครื่องมือที่จะใช้ในการทำงานแล้ว ที่ขาดไม่ได้ก็ต้องมี Tasks หรืองานให้แก่ลูกทีม

def researchFramework(self, agent):
return Task(
description=dedent("""
Analyze and summaries the provided framework. Understanding the framework, what are the methods of the framework"
"""),
expected_output=dedent("""
A comprehensive report detailing the framework and extract a framwork functions into list of
bullet point also give an example how to use of each function.
"""),
agent=agent
)

ตัว Task ก็จะประกอบไปด้วย description ก็คือคำอธิบายว่า Task นี้ต้องทำอะไรบ้าง และส่วนที่ขาดไม่ได้ก็คือ expected_output หรือเราคาดหวังว่าจะได้อะไรจาก Task นี้และ agent ก็คือ Task นี้จะให้ใครทำ

หลังจากที่ทุกอย่างพร้อมแล้ว ก็มาจัดการในส่วนของการมอบหมายงานให้กับลูกทีม

frameworkResearcherAgent = frameworkReseacher()
frameworkResearcherTask = researchFramework(agent=frameworkResearcherAgent)

crew = Crew(
agents=[
frameworkResearcherAgent
],
tasks=[
frameworkResearcherTask
],
manager_llm=ollama,
verbose=True
)

result = crew.kickoff()
print(result)

มาเริ่มกันเลยยยยย crew.kickoff() หลังจากที่รอประมาณ 300sec ผลลัพธ์ก็จะได้ประมาณนี้

จากรูปด้านบนจะเห็นได้ว่ามีการดาวโหลด Code มาจาก Github เพื่อที่จะนำข้อมูลมาทำตัวอย่างและสรุปให้เรา ซึ่งตัว Agent และ Task ด้านบนที่เราสร้างขึ้นมา ไม่รู้ด้วยซ้ำว่าเราใส่ FastAPI Framework path จาก Github เข้าไป

สรุป

หลังจากที่ได้ทดลองใช้มาและลองทำตามหลายๆ example projects, CrewAI ก็เป็นเครื่องมือที่สามารถช่วยเราได้หลายอย่าง เช่นการทำ presentation โดยที่เรา input ให้แค่หัวข้อ แล้ว Agent1 สรุปข้อมูล ส่วน Agent2 ก็นำข้อมูลสรุปที่ได้จาก Agent1 มาทำ presentation ให้เราได้เลยทันที่ แทนที่เราจะต้องหาข้อมูลเองและออกแบบข้อมูลที่อยู่ใน presentation

หรือว่าจะเป็นการสร้าง Retrieval Augmented Generation (RAG) เองได้ผ่าน Tools ที่มีให้ได้ง่ายๆ ด้วย CrewAI[Tools] แล้วนำข้อมูลมาใช้อ้างอิงเพื่อตอบคำถามเฉพาะในสายงานหรือในองค์กรอีกด้วย

ตัวแปรหลักในการที่เราจะดึงประสิทธิภาพของ LLM ให้ออกมาได้มากที่สุดตอนนี้จะขึ้นอยู่กับ Prompt ที่เราใส่เข้าไปว่ามีรายละเอียดมากแค่ไหน และอธิบายความต้องการของเราที่ตรงจุด

Ref

--

--