Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save serefyarar/5d1751430e19df683ab036485f251293 to your computer and use it in GitHub Desktop.
Save serefyarar/5d1751430e19df683ab036485f251293 to your computer and use it in GitHub Desktop.
import os
from langchain.agents import Tool
from langchain.tools.retriever import create_retriever_tool
from langchain_openai import OpenAIEmbeddings
from indexclient import IndexChroma
from crewai import Agent, Task, Crew, Process
# TODO
# I had to install langchain_chroma seperately. It should be included in the requirements
# I had to find how to import `indexclient` instead of `indexnetwork-sdk`, we need to update docs
# Sources parameter should be mandatory
os.environ["OPENAI_MODEL_NAME"]="gpt-4o"
os.environ['OPENAI_API_KEY'] = 'YOUR_OPENAI_KEY'
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
vectorstore = IndexChroma(embedding_function=embeddings, network="mainnet")
tool = create_retriever_tool(
vectorstore.as_retriever( search_kwargs={'filter': {'indexId':'kjzl6kcym7w8y806zfnn4zymjuk1acvljrjc9qnwpfaqvfkwi4yyw071h7pb0wv'}}),
"index_retriever",
"Searches and returns documents.",
)
# Creating a senior researcher agent with memory and verbose mode
researcher = Agent(
role='Senior Researcher',
goal='Provide comperhensive and complete responses in {topic}',
verbose=True,
memory=True,
backstory=(
"An expert analyst with a keen eye."
),
tools=[tool],
allow_delegation=True
)
# Creating a writer agent with custom tools and delegation capability
writer = Agent(
role='Writer',
goal='Narrate the research about {topic}',
verbose=True,
memory=True,
backstory=(
"With a flair for simplifying complex topics, you craft"
"engaging narratives that captivate and educate, bringing new"
"discoveries to light in an accessible manner."
),
tools=[tool],
allow_delegation=False
)
# Research task
research_task = Task(
description=(
"Identify the elements of {topic}."
"Focus on identifying pros and cons and the overall narrative."
"Your final report should clearly articulate the key points."
),
expected_output='A comprehensive 3 paragraphs long report ',
tools=[tool],
agent=researcher,
)
# Writing task with language model configuration
write_task = Task(
description=(
"Compose an insightful article on {topic}."
"This article should be easy to understand, engaging, and positive."
),
expected_output='A 4 paragraph article on {topic} formatted as markdown.',
tools=[tool],
agent=writer,
async_execution=False,
output_file='new-blog-post.md' # Example of output customization
)
# Forming the tech-focused crew with some enhanced configurations
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_task],
process=Process.sequential, # Optional: Sequential task execution is default
memory=True,
cache=True,
max_rpm=100,
share_crew=True
)
result = crew.kickoff(inputs={'topic': 'Differences between the cross-chain solutions'})
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment