Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using Facts in CrewAI

Introduction

In CrewAI, facts are used to store information that can be referenced and manipulated throughout your AI logic. Facts are essentially variables that can hold different types of data, such as numbers, strings, lists, or even complex objects. Understanding how to effectively use facts is crucial for creating dynamic and responsive AI behaviors.

Creating Facts

Facts can be created and assigned values using the set_fact command. This command assigns a value to a fact, which can then be used in decision-making and other AI processes.

Example:
set_fact('enemy_health', 100)

In this example, we create a fact named enemy_health and assign it a value of 100. This fact can now be used in subsequent logic to track the enemy's health.

Using Facts in Conditions

Facts can be used in conditions to make decisions based on their values. This is achieved using conditional statements that evaluate the value of a fact and execute different actions based on the result.

Example:
if get_fact('enemy_health') > 50:
print('Enemy is healthy')
else:
print('Enemy is weak')

In this example, we use the get_fact command to retrieve the value of the enemy_health fact. If the value is greater than 50, we print "Enemy is healthy"; otherwise, we print "Enemy is weak".

Updating Facts

Facts can be updated dynamically as the AI logic progresses. This is useful for tracking changes in state and making real-time decisions.

Example:
set_fact('enemy_health', get_fact('enemy_health') - 20)

In this example, we update the enemy_health fact by reducing its value by 20. This could represent the enemy taking damage during a combat encounter.

Deleting Facts

Sometimes, it may be necessary to remove a fact when it is no longer needed. This can be done using the del_fact command.

Example:
del_fact('enemy_health')

In this example, we delete the enemy_health fact, removing it from the AI's knowledge base.

Example Scenario

Let's put it all together with a comprehensive example. Consider a scenario where an AI character needs to decide whether to attack or flee based on the enemy's health and the AI's own health.

Example:
set_fact('enemy_health', 100)
set_fact('my_health', 60)

if get_fact('enemy_health') > 50 and get_fact('my_health') > 30:
print('Attack the enemy')
else:
print('Flee from the enemy')

set_fact('enemy_health', get_fact('enemy_health') - 20)

if get_fact('enemy_health') <= 0:
del_fact('enemy_health')
print('Enemy defeated')

In this example, the AI first sets initial health values for itself and the enemy. It then decides to attack or flee based on these values. After attacking, the enemy's health is reduced, and if the enemy's health drops to zero or below, it is considered defeated, and the fact is deleted.

Conclusion

Using facts in CrewAI is a powerful way to manage and manipulate data within your AI logic. By creating, using, updating, and deleting facts, you can create complex and dynamic behaviors that respond to the changing state of the game or environment. With the examples and explanations provided in this tutorial, you should be well-equipped to start using facts effectively in your CrewAI projects.