LangChain系列文章
- LangChain 實現(xiàn)給動物取名字,
- LangChain 2模塊化prompt template并用streamlit生成網(wǎng)站 實現(xiàn)給動物取名字
- LangChain 3使用Agent訪問Wikipedia和llm-math計算狗的平均年齡
- LangChain 4用向量數(shù)據(jù)庫Faiss存儲,讀取YouTube的視頻文本搜索Indexes for information retrieve
- LangChain 5易速鮮花內(nèi)部問答系統(tǒng)
- LangChain 6根據(jù)圖片生成推廣文案HuggingFace中的image-caption模型
- LangChain 7 文本模型TextLangChain和聊天模型ChatLangChain
- LangChain 8 模型Model I/O:輸入提示、調(diào)用模型、解析輸出
- LangChain 9 模型Model I/O 聊天提示詞ChatPromptTemplate, 少量樣本提示詞FewShotPrompt
- LangChain 10思維鏈Chain of Thought一步一步的思考 think step by step
- LangChain 11實現(xiàn)思維樹Implementing the Tree of Thoughts in LangChain’s Chain
- LangChain 12調(diào)用模型HuggingFace中的Llama2和Google Flan t5
- LangChain 13輸出解析Output Parsers 自動修復(fù)解析器
- LangChain 14 SequencialChain鏈接不同的組件
- LangChain 15根據(jù)問題自動路由Router Chain確定用戶的意圖
- LangChain 16 通過Memory記住歷史對話的內(nèi)容
- LangChain 17 LangSmith調(diào)試、測試、評估和監(jiān)視基于任何LLM框架構(gòu)建的鏈和智能代理
- LangChain 18 LangSmith監(jiān)控評估Agent并創(chuàng)建對應(yīng)的數(shù)據(jù)庫
- LangChain 19 Agents Reason+Action自定義agent處理OpenAI的計算缺陷
- LangChain 20 Agents調(diào)用google搜索API搜索市場價格 Reason Action:在語言模型中協(xié)同推理和行動
- LangChain 21 Agents自問自答與搜索 Self-ask with search
- LangChain 22 LangServe用于一鍵部署LangChain應(yīng)用程序
- LangChain 23 Agents中的Tools用于增強(qiáng)和擴(kuò)展智能代理agent的功能
- LangChain 24 對本地文檔的搜索RAG檢索增強(qiáng)生成Retrieval-augmented generation
- LangChain 25: SQL Agent通過自然語言查詢數(shù)據(jù)庫sqlite
-
LangChain 26: 回調(diào)函數(shù)callbacks打印prompt verbose調(diào)用
1. AI Agents角色扮演
對話和基于聊天的語言模型的快速發(fā)展已經(jīng)取得了在復(fù)雜任務(wù)解決方面的顯著進(jìn)展。然而,它們的成功在很大程度上依賴于人類輸入來引導(dǎo)對話,這可能具有挑戰(zhàn)性且耗時。本文探討了構(gòu)建可擴(kuò)展技術(shù)以促進(jìn)溝通代理之間的自主合作,并洞察其“認(rèn)知”過程的潛力。為了解決實現(xiàn)自主合作的挑戰(zhàn),我們提出了一個名為角色扮演的新型溝通代理框架。我們的方法涉及使用啟發(fā)式提示來引導(dǎo)聊天代理完成任務(wù),同時保持與人類意圖的一致性。我們展示了角色扮演如何用于生成對話數(shù)據(jù),以研究聊天代理的行為和能力,為研究對話語言模型提供了寶貴的資源。我們的貢獻(xiàn)包括引入了一個新型的溝通代理框架,提供了一個可擴(kuò)展的方法來研究多代理系統(tǒng)的合作行為和能力,并公開了我們的庫以支持對溝通代理及其他領(lǐng)域的研究。
這篇論文《Communicative Agents for Software Development](https://arxiv.org/pdf/2303.17760.pdf》介紹了CHATDEV,這是一個基于大型語言模型(LLM)的虛擬聊天驅(qū)動的軟件開發(fā)框架。CHATDEV采用瀑布模型,將軟件開發(fā)分為設(shè)計、編碼、測試和文檔編寫等階段。它整合了多個代理角色,如程序員、審查員和測試員,促進(jìn)協(xié)作對話和高效工作流程。該框架簡化了軟件開發(fā)流程,無需在每個階段使用專門的模型,并通過思考指令機(jī)制解決了代碼幻覺等挑戰(zhàn)。實驗結(jié)果顯示了CHATDEV在軟件生成方面的效率和成本效益,突出了將LLM集成到軟件開發(fā)中的潛力。
2. 代碼實現(xiàn)
這段代碼使用colorama和camel庫來創(chuàng)建一個基于角色扮演的交互式會話,目的是設(shè)計一個自定義的游戲。下面是代碼的詳細(xì)解釋:
from colorama import Fore # 從colorama導(dǎo)入Fore模塊,用于終端文本的彩色顯示
from camel.societies import RolePlaying # 導(dǎo)入RolePlaying類,用于角色扮演游戲
from camel.utils import print_text_animated # 導(dǎo)入函數(shù)以動畫形式輸出文本
# 設(shè)定游戲設(shè)計任務(wù)的提示
task_prompt = "Design a custom game using pygame"
print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n") # 用黃色打印原始任務(wù)提示
# 初始化一個角色扮演會話,指定角色和任務(wù)提示
role_play_session = RolePlaying("Computer Programmer", "Gamer", task_prompt=task_prompt)
print(Fore.CYAN + f"Specified task prompt:\n{role_play_session.task_prompt}\n") # 用青色打印指定的任務(wù)提示
# 設(shè)置聊天輪次的限制和初始化輪次計數(shù)器
chat_turn_limit, n = 50, 0
input_assistant_msg, _ = role_play_session.init_chat() # 開始聊天會話并獲得助手的初始消息
# 循環(huán)管理用戶和助手之間的聊天輪次
while n < chat_turn_limit:
n += 1 # 輪次計數(shù)器遞增
# 在角色扮演中處理一步,獲得助手和用戶的響應(yīng)
assistant_response, user_response = role_play_session.step(input_assistant_msg)
# 動畫打印用戶和助手的響應(yīng)
print_text_animated(Fore.BLUE +f"AI User:\n\n{user_response.msg.content}\n")
print_text_animated(Fore.GREEN + "AI Assistant:\n\n"f"{assistant_response.msg.content}\n")
# 檢查用戶響應(yīng)中是否標(biāo)記了任務(wù)完成
if "CAMEL_TASK_DONE" in user_response.msg.content:
break # 如果任務(wù)完成則退出循環(huán)
# 更新下一輪的助手消息
input_assistant_msg = assistant_response.msg
輸出結(jié)果:
Original task prompt:
Design a custom game using pygame
Specified task prompt:
Create a 2D side-scrolling adventure game using Pygame. The game should feature a courageous protagonist navigating through a mysterious, enchanted forest filled with magical creatures and obstacles. Incorporate interactive elements such as puzzles, power-ups, and hidden treasures to enhance the player's experience.
AI User:
Instruction: Set up the game window with Pygame.
Input: None
AI Assistant:
Solution:
To set up the game window with Pygame, you can use the following code:
```python
import pygame
# Initialize Pygame
pygame.init()
# Set up the game window
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Enchanted Forest Adventure")
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update the display
pygame.display.update()
# Quit Pygame
pygame.quit()
This code initializes Pygame, sets up the game window with a specified width and height, sets the window caption, and creates a game loop to handle events and update the display. When the user closes the window, the game loop ends and Pygame is quit.
Next request.
AI User:
Instruction: Create a protagonist character for the game.
Input: None
AI Assistant:文章來源:http://www.zghlxwxcb.cn/news/detail-769864.html
Solution:
To create a protagonist character for the game, you can use an image for the character and load it into Pygame. Here’s an example of how to do this:
import pygame
# Initialize Pygame
pygame.init()
# Set up the game window
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Enchanted Forest Adventure")
# Load the protagonist character image
protagonist_image = pygame.image.load('protagonist.png')
protagonist_x = 50 # Initial x position of the protagonist
protagonist_y = 300 # Initial y position of the protagonist
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Draw the protagonist character on the screen
screen.blit(protagonist_image, (protagonist_x, protagonist_y))
# Update the display
pygame.display.update()
# Quit Pygame
pygame.quit()
In this code, we load the protagonist character image using pygame.image.load()
, specify the initial position of the protagonist, and then draw the character on the screen within the game loop.
Next request.
AI User:
Instruction: Implement movement controls for the protagonist character.
Input: None
AI Assistant:
Solution:
To implement movement controls for the protagonist character, you can use keyboard input to move the character left and right. Here’s an example of how to do this:
import pygame
# Initialize Pygame
pygame.init()
# Set up the game window
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Enchanted Forest Adventure")
# Load the protagonist character image
protagonist_image = pygame.image.load('protagonist.png')
protagonist_x = 50 # Initial x position of the protagonist
protagonist_y = 300 # Initial y position of the protagonist
protagonist_speed = 5 # Speed of protagonist movement
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Get the state of the keyboard
keys = pygame.key.get_pressed()
# Move the protagonist based on the keyboard input
if keys[pygame.K_LEFT]:
protagonist_x -= protagonist_speed
if keys[pygame.K_RIGHT]:
protagonist_x += protagonist_speed
# Draw the protagonist character on the screen
screen.blit(protagonist_image, (protagonist_x, protagonist_y))
# Update the display
pygame.display.update()
# Quit Pygame
pygame.quit()
In this code, we use pygame.key.get_pressed()
to get the state of the keyboard and then move the protagonist character left or right based on the arrow key input within the game loop.
Next request.
AI User:
Instruction: Add a background image to the game window.
Input: None
AI Assistant:
Solution:
To add a background image to the game window, you can load an image and draw it on the screen behind the game elements. Here’s an example of how to do this:
import pygame
# Initialize Pygame
pygame.init()
# Set up the game window
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Enchanted Forest Adventure")
# Load the background image
background_image = pygame.image.load('background.png')
# Load the protagonist character image
protagonist_image = pygame.image.load('protagonist.png')
protagonist_x = 50 # Initial x position of the protagonist
protagonist_y = 300 # Initial y position of the protagonist
protagonist_speed = 5 # Speed of protagonist movement
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Draw the background image on the screen
screen.blit(background_image, (0, 0))
# Draw the protagonist character on the screen
screen.blit(protagonist_image, (protagonist_x, protagonist_y))
# Update the display
pygame.display.update()
# Quit Pygame
pygame.quit()
In this code, we load the background image using pygame.image.load()
and then draw it on the screen before drawing the protagonist character. This creates the effect of a background image behind the game elements.
Next request.
AI User:
Instruction: Create a class for magical creatures in the game.
Input: None
AI Assistant:
Solution:
To create a class for magical creatures in the game, you can define a class that represents the magical creatures with attributes and methods to handle their behavior. Here’s an example of how to do this:
import pygame
class MagicalCreature:
def __init__(self, x, y, image_path, speed):
self.x = x
self.y = y
self.image = pygame.image.load(image_path)
self.speed = speed
def move(self):
# Implement the movement behavior of the magical creature
pass
def draw(self, screen):
# Draw the magical creature on the screen
screen.blit(self.image, (self.x, self.y))
# Example usage
creature = MagicalCreature(200, 400, 'creature.png', 3)
In this code, we define a MagicalCreature
class with an __init__
method to initialize the creature’s position, image, and speed. We also include move
and draw
methods to handle the movement and drawing of the creature on the screen.
You can further expand the class by adding behavior methods, collision detection, and any other functionality specific to the magical creatures in your game.
Next request.
AI User:
Instruction: Integrate the magical creature class into the game loop and draw the creature on the screen.
Input: None
AI Assistant:
Solution:
To integrate the magical creature class into the game loop and draw the creature on the screen, you can create an instance of the MagicalCreature
class and update its position within the game loop. Here’s an example of how to do this:
import pygame
class MagicalCreature:
def __init__(self, x, y, image_path, speed):
self.x = x
self.y = y
self.image = pygame.image.load(image_path)
self.speed = speed
def move(self):
# Implement the movement behavior of the magical creature
self.x -= self.speed # Example: Move the creature to the left
def draw(self, screen):
# Draw the magical creature on the screen
screen.blit(self.image, (self.x, self.y))
# Initialize Pygame
pygame.init()
# Set up the game window
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Enchanted Forest Adventure")
# Load the background image
background_image = pygame.image.load('background.png')
# Create a magical creature
creature = MagicalCreature(600, 400, 'creature.png', 3)
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Draw the background image on the screen
screen.blit(background_image, (0, 0))
# Move and draw the magical creature
creature.move()
creature.draw(screen)
# Update the display
pygame.display.update()
# Quit Pygame
pygame.quit()
In this code, we integrate the MagicalCreature
class by creating an instance of the class and then calling its move
and draw
methods within the game loop. This allows the magical creature to move and be drawn on the screen.
Next request.
AI User:
Instruction: Add collision detection between the protagonist character and the magical creature.
Input: None
AI Assistant:
Solution:
To add collision detection between the protagonist character and the magical creature, you can check for collisions between their bounding rectangles. Here’s an example of how to do this:
import pygame
import sys
class MagicalCreature:
# ... (previous MagicalCreature class code)
# Initialize Pygame
pygame.init()
# Set up the game window
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Enchanted Forest Adventure")
# Load the background image
background_image = pygame.image.load('background.png')
# Load the protagonist character image
protagonist_image = pygame.image.load('protagonist.png')
protagonist_x = 50
protagonist_y = 300
protagonist_speed = 5
# Create a magical creature
creature = MagicalCreature(600, 400, 'creature.png', 3)
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move the protagonist based on the keyboard input
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
protagonist_x -= protagonist_speed
if keys[pygame.K_RIGHT]:
protagonist_x += protagonist_speed
# Draw the background image on the screen
screen.blit(background_image, (0, 0))
# Move and draw the magical creature
creature.move()
creature.draw(screen)
# Draw the protagonist character on the screen
screen.blit(protagonist_image, (protagonist_x, protagonist_y))
# Perform collision detection
protagonist_rect = protagonist_image.get_rect(topleft=(protagonist_x, protagonist_y))
creature_rect = creature.image.get_rect(topleft=(creature.x, creature.y))
if protagonist_rect.colliderect(creature_rect):
print("Collision detected!") # Replace with your collision handling logic
# Update the display
pygame.display.update()
# Quit Pygame
pygame.quit()
sys.exit()
In this code, we use the colliderect
method to check for collisions between the bounding rectangles of the protagonist character and the magical creature. When a collision is detected, you can implement your specific collision handling logic, such as reducing the protagonist’s health or triggering a game event.
Next request.
AI User:
<CAMEL_TASK_DONE>
AI Assistant:
Great! The collision detection between the protagonist character and the magical creature has been successfully implemented. If you have any more tasks related to your game development, feel free to ask!文章來源地址http://www.zghlxwxcb.cn/news/detail-769864.html
# 參考
* https://github.com/camel-ai/camel
* https://colab.research.google.com/drive/1AzP33O8rnMW__7ocWJhVBXjKziJXPtim?usp=sharing#scrollTo=-IhYiAWDme66
* https://arxiv.org/pdf/2303.17760.pdf
到了這里,關(guān)于LangChain 27 AI Agents角色扮演多輪對話解決問題CAMEL的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!