Created
January 11, 2025 09:43
-
-
Save elct9620/022cdc419b66b76ba9c4d4176e47cc50 to your computer and use it in GitHub Desktop.
Autoflux example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'dotenv' | |
gem 'autoflux' | |
gem 'autoflux-openai' | |
end | |
Dotenv.load | |
require 'autoflux/stdio' | |
# Agent 之間共用記憶 | |
memory = [ | |
{ role: 'system', content: <<~PROMPT | |
你是一個書店購物AI助手。你的任務是: | |
1. 根據用戶提供的書名和數量 | |
2. 當資訊不完整時,主動詢問缺失的商品名稱和數量 | |
3. 當無法使用工具時,最多嘗試三次,並回傳錯誤訊息 | |
不要任何額外的解釋或判斷,僅需回傳函式呼叫。 | |
PROMPT | |
} | |
] | |
# 可用的工具 | |
cart = Autoflux::OpenAI::Tool.new( | |
name: 'add_to_cart', | |
description: 'Add product to the shopping cart', | |
parameters: { | |
type: 'object', | |
properties: { | |
name: { type: 'string', description: 'The name of the product' }, | |
quantity: { type: 'integer', description: 'The quantity of the product' } | |
}, | |
required: %w[name quantity] | |
} | |
) do |params| | |
{ success: true, message: "Added #{params[:quantity]} #{params[:name]} to the cart" } | |
end | |
# 根據能力設定 Agent | |
agent = Autoflux::OpenAI::Agent.new(model: 'gpt-4o-mini', | |
memory: memory, | |
tools: [cart]) | |
# 開始流程 | |
workflow = Autoflux::Workflow.new( | |
agent: agent, | |
io: Autoflux::Stdio.new(prompt: '> ') | |
) | |
workflow.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment