|
| 1 | +import os |
| 2 | +from crewai import Agent, Task, Crew, Process |
| 3 | +from langchain_community.llms import Ollama |
| 4 | +from notion_client import Client |
| 5 | +from langchain_community.utilities.google_search import GoogleSearchAPIWrapper |
| 6 | +from langchain.tools import DuckDuckGoSearchRun |
| 7 | + |
| 8 | +# Initialize the Notion client with your integration token |
| 9 | +notion = Client(auth="YOUR NOTION API KEY") |
| 10 | +database_id = "6179c6f0db6d47f9be0c1eb97c088c15" # Replace with your database ID |
| 11 | +note_title = "CS Online Bachelor's Degrees" |
| 12 | + |
| 13 | +# Function to add a note to the database |
| 14 | +def add_note_to_database(database_id, title, content): |
| 15 | + new_page_data = { |
| 16 | + "parent": {"database_id": database_id}, |
| 17 | + "properties": { |
| 18 | + "Title": { |
| 19 | + "title": [ |
| 20 | + { |
| 21 | + "text": { |
| 22 | + "content": title |
| 23 | + } |
| 24 | + } |
| 25 | + ] |
| 26 | + } |
| 27 | + }, |
| 28 | + "children": [ |
| 29 | + { |
| 30 | + "object": "block", |
| 31 | + "type": "paragraph", |
| 32 | + "paragraph": { |
| 33 | + "rich_text": [ |
| 34 | + { |
| 35 | + "type": "text", |
| 36 | + "text": { |
| 37 | + "content": content |
| 38 | + } |
| 39 | + } |
| 40 | + ] |
| 41 | + } |
| 42 | + } |
| 43 | + ] |
| 44 | + } |
| 45 | + # Use the Notion SDK to create a new page in the database |
| 46 | + notion.pages.create(**new_page_data) |
| 47 | + |
| 48 | +# You can choose to use a local model through Ollama for example. |
| 49 | +# |
| 50 | +# from langchain.llms import Ollama |
| 51 | +ollama_llm = Ollama(model="openhermes") |
| 52 | + |
| 53 | +# Use DuckDuckGoSearchRun as a tool |
| 54 | +search_tool = DuckDuckGoSearchRun() |
| 55 | + |
| 56 | +# Define your agents with roles and goals |
| 57 | +researcher = Agent( |
| 58 | + role='Senior Research Analyst', |
| 59 | + goal='Find prestigious and affordable unviversities that offer online Bachelor\'s degrees in Computer Science', |
| 60 | + backstory="""You are a guidance counselor at a high school in the US. |
| 61 | + You are helping a student find a prestigious and affordable university. |
| 62 | + The student is interested in pursuing a Bachelor's degree in Computer Science. |
| 63 | + The student is open to online programs, but they must be from a reputable university.""", |
| 64 | + verbose=True, |
| 65 | + allow_delegation=True, |
| 66 | + tools=[search_tool], |
| 67 | + llm=ollama_llm |
| 68 | + # You can pass an optional llm attribute specifying what mode you wanna use. |
| 69 | + # It can be a local model through Ollama / LM Studio or a remote |
| 70 | + # model like OpenAI, Mistral, Antrophic of others (https://python.langchain.com/docs/integrations/llms/) |
| 71 | + # |
| 72 | + # Examples: |
| 73 | + # llm=ollama_llm # was defined above in the file |
| 74 | + # llm=ChatOpenAI(model_name="gpt-3.5", temperature=0.7) |
| 75 | +) |
| 76 | +writer = Agent( |
| 77 | + role='University Admissions Officer', |
| 78 | + goal='Craft a compelling email to the student that lists the top 3 universities that offer online Bachelor\'s degrees in Computer Science', |
| 79 | + backstory="""You are an admissions officer at a prestigious university. |
| 80 | + You are responding to a student who is interested in pursuing a Bachelor's degree in Computer Science. |
| 81 | + The student is open to online programs, but they must be from a reputable university.""", |
| 82 | + verbose=True, |
| 83 | + allow_delegation=True, |
| 84 | + llm=ollama_llm |
| 85 | +) |
| 86 | + |
| 87 | +# Create tasks for your agents |
| 88 | +task1 = Task( |
| 89 | + description="""Conduct a comprehensive analysis of the available online Bachelor's degrees in Computer Science.""", |
| 90 | + agent=researcher |
| 91 | +) |
| 92 | + |
| 93 | +task2 = Task( |
| 94 | + description="""Using the insights provided, develop an email that lists the top 3 universities that offer online Bachelor's degrees in Computer Science.""", |
| 95 | + agent=writer |
| 96 | +) |
| 97 | + |
| 98 | +# Instantiate your crew with a sequential process |
| 99 | +crew = Crew( |
| 100 | + agents=[researcher, writer], |
| 101 | + tasks=[task1, task2], |
| 102 | + verbose=2, # You can set it to 1 or 2 to different logging levels |
| 103 | +) |
| 104 | + |
| 105 | +# Get your crew to work! |
| 106 | +result = crew.kickoff() |
| 107 | + |
| 108 | +print("######################") |
| 109 | +print(result) |
| 110 | + |
| 111 | +# Add the result to the database |
| 112 | +add_note_to_database(database_id, note_title, result) |
0 commit comments