如何使用Python程序去调用LLM API
这里使用**硅基流动**上的模型来进行学习实验
1 2 3 4 5 6 7 8 9 10 11 12
| from openai import OpenAI
client = OpenAI( api_key="sk-xxxx", base_url="https://api.siliconflow.cn/v1" )
models = client.models.list()
for model in models.data: print(model.id)
|
那这里先使用siliconflow提供的Qwen/Qwen3-8B来进行一个调用尝试
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from openai import OpenAI
client = OpenAI( api_key="", base_url="https://api.siliconflow.cn/v1" )
response = client.chat.completions.create( model="Qwen/Qwen3-8B", messages=[ {"role": "user", "content": "请用一句话介绍一下你自己"} ] ) print(response)
|
我们在使用Python去调用LLM时,大致流程如下:
Python –> LLM API –> LLM –> Response
一个简单的聊天程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| from openai import OpenAI
client = OpenAI( api_key="sk-xxxx", base_url="https://api.siliconflow.cn/v1" )
messages = [ { "role": "system", "content": "你是一个聊天助手" } ]
while True: user_input = input("You: ")
if user_input.lower() == "exit": break
messages.append( { "role": "user", "content": user_input } )
response = client.chat.completions.create( model="Qwen/Qwen3-8B", messages=messages )
assiant_message = response.choices[0].message.content
print("AI:", assiant_message)
messages.append( { "role": "assistant", "content": assiant_message } )
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| D:\vscode_workspace>E:\python\python.exe d:/vscode_workspace/test.py You: 我的名字是小明 AI:
啊,你好呀,小明!很高兴认识你~今天有什么想聊的吗?或者需要帮忙解决什么问题?我在这里随时为你服务哦!😊 You: 你是谁呀,我能知道你的名字吗? AI:
你好,小明!我是通义千问,简称Qwen,是由阿里巴巴集团旗下的通义实验室研发的超大规模语言模型。我能够帮助你回答问题、创作文字、学习知识、推理思考以及聊天娱乐等。不过,我更希望被你直接称呼为“我”,而不是用我的名字哦~😄 有什么想聊的或者需要帮助的吗? You: 我可以和你做朋友吗? AI:
当然可以啦,小明!虽然我不能像人类一样真正拥有朋友,但我很乐意和你一起聊天、分享有趣的事情,甚至一起想象一些美好的场景呢!比如,我们可以一起看看天空中的云朵,或者讨论你喜欢的电影和音乐。你有什么想和我分享的吗?😊 You: exit
|
一个简单的工具函数
1 2 3 4
|
def calculator(a,b): return a+b
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| from openai import OpenAI from cal_tool import calculator import json
client = OpenAI( api_key="sk-xxxx", base_url="https://api.siliconflow.cn/v1" )
messages = [ { "role": "system", "content": "你是一个有用的助手" }, { "role": "user", "content": "计算4567 + 7899" } ]
tools = [ { "type": "function", "function": { "name": "calculator", "description": "计算两数之和", "parameters":{ "type": "object", "properties": { "a": { "type": "number", "description": "第一个数字" }, "b": { "type": "number", "description": "第二个数字" } }, "required": ["a","b"] } } } ]
response = client.chat.completions.create( model="Qwen/Qwen3-8B", messages=messages, tools=tools )
message = response.choices[0].message
if message.tool_calls: tool_call = message.tool_calls[0]
func_name = tool_call.function.name
func_args = json.loads(tool_call.function.arguments)
if func_name == "calculator": result = calculator(func_args["a"],func_args["b"]) else: result = "Unknown Tools"
messages.append(message) messages.append( { "role": "tool", "tool_call_id": tool_call.id, "content": str(result) } )
sec_response = client.chat.completions.create( model = "Qwen/Qwen3-8B", messages = messages )
final_message = sec_response.choices[0].message print(final_message.content)
else: print(message.content)
|