微信扫码
与创始人交个朋友
我要投稿
import openai
import os
from math import *
from icecream import ic
import json
# 加载 .env 文件
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
# 从环境变量中获得你的 OpenAI Key
openai.api_key = os.getenv('OPENAI_API_KEY')
openai.api_base = os.getenv('OPENAI_API_URL')
model = os.getenv('MODEL')
# 基于 prompt 生成文本
def get_completion3(messages, model=model):
response = openai.ChatCompletion.create(
# 注意,以前的 openai.ChatCompletion 要换成 openai.chat.completions
model=model,
messages=messages,
temperature=0,# 模型输出的随机性,0 表示随机性最小
tools=[{# 用 JSON 描述函数。可以定义多个。由大模型决定调用谁
"type": "function",
"function": {
"name": "sum",
"description": "计算一组数的加和",
"parameters": {
"type": "object",
"properties": {
"numbers": {
"type": "array",
"items": {
"type": "number"
}
}
}
}
}
}],
)
return response.choices[0].message
def test_promopt():
prompt = "Tell me the sum of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10."
#prompt = "桌上有 2 个苹果,四个桃子和 3 本书,一共有几个水果?"
#prompt = "1+2+3...+99+100"
messages = [
{"role": "system", "content": "你是一个小学数学老师,你要教学生加法"},
{"role": "user", "content": prompt}
]
response = get_completion3(messages)
# 把大模型的回复加入到对话历史中
if (response.content is None):# 解决 OpenAI 的一个 400 bug
response.content = "null"
messages.append(response)
# 如果返回的是函数调用结果,则打印出来
if (response.tool_calls is not None):
# 是否要调用 sum
tool_call = response.tool_calls[0]
if (tool_call.function.name == "sum"):
# 调用 sum
args = json.loads(tool_call.function.arguments)
result = sum(args["numbers"])
# 把函数调用结果加入到对话历史中
messages.append(
{
"tool_call_id": tool_call.id,# 用于标识函数调用的 ID
"role": "tool",
"name": "sum",
"content": str(result)# 数值result 必须转成字符串
}
)
# 再次调用大模型
ic("=====最终回复=====")
ic(get_completion3(messages).content)
if __name__ == '__main__':
test_promopt()
ic| '=====最终回复====='ic| get_completion3(messages).content: 'The sum of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 is 55.'
import openai
import os
from math import *
from icecream import ic
import json
from math import *
# 加载 .env 文件
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
# 从环境变量中获得你的 OpenAI Key
openai.api_key = os.getenv('OPENAI_API_KEY')
openai.api_base = os.getenv('OPENAI_API_URL')
model = os.getenv('MODEL')
# 基于 prompt 生成文本
def get_completion(messages, model=model):
response = openai.ChatCompletion.create(
# 注意,以前的 openai.ChatCompletion 要换成 openai.chat.completions
model=model,
messages=messages,
temperature=0,# 模型输出的随机性,0 表示随机性最小
tools=[{
"type": "function",
"function": {
"name": "calculate",
"description": "计算一个数学表达式的值",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "a mathematical expression in python grammar.",
}
}
}
}
}],
)
return response.choices[0].message
def test_promopt():
#prompt = "从1加到10"
prompt = "3的平方根乘以2再开平方"
messages = [
{"role": "system", "content": "你是一个数学家,你可以计算任何算式。"},
{"role": "user", "content": prompt}
]
response = get_completion(messages)
if (response.content is None):# 解决 OpenAI 的一个 400 bug
response.content = "null"
messages.append(response)# 把大模型的回复加入到对话中
ic("=====GPT回复=====")
ic(response)
# 如果返回的是函数调用结果,则打印出来
if (response.tool_calls is not None):
# 是否要调用 sum
tool_call = response.tool_calls[0]
if (tool_call.function.name == "calculate"):
# 调用 calculate
args = json.loads(tool_call.function.arguments)
result = eval(args["expression"])
ic("=====函数返回=====")
ic(result)
# 把函数调用结果加入到对话历史中
messages.append(
{
"tool_call_id": tool_call.id,# 用于标识函数调用的 ID
"role": "tool",
"name": "calculate",
"content": str(result)# 数值result 必须转成字符串
}
)
# 再次调用大模型
ic("=====最终回复=====")
ic(get_completion(messages).content)
if __name__ == '__main__':
test_promopt()
ic| '=====GPT回复====='ic| response: <OpenAIObject at 0x1a97b6d3480> JSON: {"role": "assistant","content": "null","tool_calls": [{"id": "call_jSVjTMqMIMUHYCpDisvpYInp","type": "function","function": {"name": "calculate","arguments": "{\"expression\":\"((3**0.5)*2)**0.5\"}"}}]}ic| '=====函数返回====='ic| result: 1.8612097182041991ic| '=====最终回复====='ic| get_completion(messages).content: '3的平方根乘以2再开平方的结果约为1.8612。'
import openai
import os
from math import *
from icecream import ic
import json
from math import *
import requests
import logging
# 设置日志记录配置
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 加载 .env 文件
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
# 从环境变量中获得你的 OpenAI Key
openai.api_key = os.getenv('OPENAI_API_KEY')
openai.api_base = os.getenv('OPENAI_API_URL')
model = os.getenv('MODEL')
amap_key = os.getenv('GAODE_MAP_API_KEY')
def get_completion(messages, model=model):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,# 模型输出的随机性,0 表示随机性最小
tools=[{
"type": "function",
"function": {
"name": "add_contact",
"description": "添加联系人",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "联系人姓名"
},
"address": {
"type": "string",
"description": "联系人地址"
},
"tel": {
"type": "string",
"description": "联系人电话"
},
}
}
}
}],
)
return response.choices[0].message
def test_promopt():
prompt = "帮我寄给星城老谭,地址是湖南省长沙市岳麓区湘江壹号8楼,电话18874868888。"
messages = [
{"role": "system", "content": "你是一个联系人录入员。"},
{"role": "user", "content": prompt}
]
response = get_completion(messages)
logging.info("====GPT回复====")
logging.info(response)
args = json.loads(response.tool_calls[0].function.arguments)
logging.info("====函数参数====")
logging.info(args)
if __name__ == '__main__':
test_promopt()
2024-06-05 13:31:30,121 - INFO - ====GPT回复====2024-06-05 13:31:30,124 - INFO - {"role": "assistant","content": null,"tool_calls": [{"id": "call_QEnFRbZRBs2Eww4YbUxP7oKv","type": "function","function": {"name": "add_contact","arguments": "{\"name\":\"\u661f\u57ce\u8001\u8c2d\",\"address\":\"\u6e56\u5357\u7701\u957f\u6c99\u5e02\u5cb3\u9e93\u533a\u6e58\u6c5f\u58f9\u53f78\u697c\",\"tel\":\"18874868888\"}"}}]}2024-06-05 13:31:30,124 - INFO - ====函数参数====2024-06-05 13:31:30,125 - INFO - {'name': '星城老谭', 'address': '湖南省长沙市岳麓区湘江壹号8楼', 'tel': '1887486XXXX'}
53AI,企业落地应用大模型首选服务商
产品:大模型应用平台+智能体定制开发+落地咨询服务
承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-01-08
仅需一万块钱!清华团队靠强化学习让 7B模型数学打败GPT-4o
2025-01-08
OA集成AI大模型简单实践-我把大模型集成到致远OA中系列
2025-01-08
手机「自动驾驶」大揭秘!vivo万字综述探讨大模型手机自动化
2025-01-08
不只 5090,英伟达还发布了最小的「AI 超算」与最大的世界模型
2025-01-07
OpenAI O3揭秘:特性、创新及与O1的较量
2025-01-07
大模型推理引擎国产化思考和实践
2025-01-07
AI Agents的崛起,创业公司及巨头的agents布局版图
2025-01-06
何宝宏:大语言模型上半场已进入“垃圾时间”
2024-05-28
2024-08-13
2024-04-26
2024-08-21
2024-06-13
2024-08-04
2024-07-09
2024-09-23
2024-04-11
2024-07-18