前言
文章底部有接入教程哦~,添加客服二维码,发送口令【领取福利】
,免费领取体验额度。
YIBU API 中转站实现 OpenAI Function Call 功能
为什么选择一步API中转站?
一步API中转站为开发者提供了稳定、高效的 API 调用服务,具有以下优势:
- 稳定的连接速度,确保您的租房查询请求快速响应
- 简化的接入流程,降低开发难度
- 优化的请求处理机制,提高成功率
- 专业的技术支持,解决您的开发问题
租房助手 Function Call 示例代码
以下是使用一步API中转站实现租房助手的完整示例代码:
from openai import OpenAI
import json
# 设置API密钥和基础URL
api_key = "sk-aBo2gWUzln更新成你自己的Key" # 请替换为您在一步API获取的密钥
api_base = "https://api.yibuapi.com/v1"
client = OpenAI(api_key=api_key, base_url=api_base)
# 定义租房查询函数
functions = [
{
"name": "search_rental_properties",
"description": "Search for rental properties based on user preferences",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "The city or neighborhood, e.g., 'Manhattan, NY' or 'Pudong, Shanghai'"},
"budget": {"type": "number", "description": "Maximum monthly rent in local currency"},
"bedrooms": {"type": "integer", "description": "Minimum number of bedrooms required"},
"bathrooms": {"type": "integer", "description": "Minimum number of bathrooms required"},
"amenities": {
"type": "array",
"items": {"type": "string"},
"description": "Desired amenities like 'parking', 'gym', 'pet-friendly', 'furnished', etc."
},
"commute_to": {"type": "string", "description": "Location for commute calculation (optional)"},
"max_commute_time": {"type": "integer", "description": "Maximum commute time in minutes (optional)"}
},
"required": ["location", "budget"]
}
}
]
# 用户查询
user_query = "我想在上海浦东找一个两居室的公寓,预算在每月8000元以内,最好有健身房和停车位,离陆家嘴不超过30分钟车程。"
# 生成调用请求
messages = [
{"role": "system", "content": "你是一个专业的租房助手,可以帮助用户寻找符合他们需求的租房选择。"},
{"role": "user", "content": user_query}
]
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
functions=functions,
function_call="auto"
)
# 检查是否需要调用函数
if response.choices[0].message.function_call:
function_call = response.choices[0].message.function_call
function_name = function_call.name
function_params = json.loads(function_call.arguments)
# 根据函数名称执行相应的操作
if function_name == "search_rental_properties":
# 这里应该实现search_properties函数来查询实际的租房数据
# 以下是示例返回数据
rental_data = {
"properties": [
{
"id": "prop123",
"name": "阳光花园",
"location": "浦东新区张江高科技园区",
"price": 7800,
"bedrooms": 2,
"bathrooms": 1,
"size": 85,
"amenities": ["健身房", "停车位", "24小时安保"],
"commute_to_lujiazui": "25分钟",
"available_from": "2025-07-01",
"contact": "021-12345678"
},
{
"id": "prop456",
"name": "滨江公寓",
"location": "浦东新区浦东大道",
"price": 7500,
"bedrooms": 2,
"bathrooms": 1,
"size": 78,
"amenities": ["停车位", "花园", "近地铁"],
"commute_to_lujiazui": "20分钟",
"available_from": "2025-07-15",
"contact": "021-87654321"
},
{
"id": "prop789",
"name": "东方丽景",
"location": "浦东新区金桥",
"price": 8000,
"bedrooms": 2,
"bathrooms": 2,
"size": 90,
"amenities": ["健身房", "停车位", "游泳池", "儿童乐园"],
"commute_to_lujiazui": "28分钟",
"available_from": "2025-08-01",
"contact": "021-56781234"
}
],
"total_results": 3,
"search_criteria": function_params
}
# 将结果发送回OpenAI
result_message = {
"role": "function",
"name": function_name,
"content": json.dumps(rental_data, ensure_ascii=False)
}
messages.append(response.choices[0].message)
messages.append(result_message)
final_response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
print(final_response.choices[0].message.content)
else:
print(response.choices[0].message.content)
代码详解
1. 初始化设置
from openai import OpenAI
import json
api_key = "您的YIBU API密钥"
api_base = "https://api.apiyi.com/v1"
client = OpenAI(api_key=api_key, base_url=api_base)
这部分代码完成了必要的库导入和客户端初始化。通过设置base_url为一步API 的地址,您可以享受到稳定高效的 API 服务。
2. 定义租房查询函数
functions = [
{
"name": "search_rental_properties",
"description": "Search for rental properties based on user preferences",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "The city or neighborhood"},
"budget": {"type": "number", "description": "Maximum monthly rent"},
# 其他参数...
},
"required": ["location", "budget"]
}
}
]
这里定义了一个专门用于租房查询的函数,包含了位置、预算、卧室数量、设施等多种参数,使 AI 能够准确理解用户需求。
3. 处理用户查询
当用户提出租房需求时,AI 会分析请求并调用相应的函数来检索合适的房源信息。最后,AI 会整理这些信息,以易于理解的方式呈现给用户。
实际应用场景
- 房地产网站和应用:提供智能化的租房推荐服务
- 移动端租房助手:用户只需描述需求,即可获取符合条件的房源
- 房产中介服务:提高客户匹配效率,减少人工筛选工作
- 大学生租房平台:针对学生特定需求提供精准推荐
如何优化您的租房助手
- 实现真实数据接口:将示例中的模拟数据替换为真实的房源数据库查询
- 添加更多筛选条件:如房龄、装修程度、周边设施等
- 整合地图功能:显示房源位置和周边交通信息
- 提供价格分析:与周边同类房源进行比较分析
总结
通过 一步API 中转站,我们可以轻松实现基于 OpenAI Function Call 的智能租房助手。立即访问 一步API 中转站开始您的开发之旅!
您已阅读完《使用指南(共8篇)》专题的第 1 篇。请继续阅读该专题下面的文章: