ChatGLM3-6B-Osborn / tool_using /openai_api_demo.py
Osborn-bh's picture
Upload folder using huggingface_hub
155ace6
import json
import openai
from loguru import logger
from tool_register import get_tools, dispatch_tool
openai.api_base = "http://localhost:8000/v1"
openai.api_key = "xxx"
tools = get_tools()
system_info = {
"role": "system",
"content": "Answer the following questions as best as you can. You have access to the following tools:",
"tools": list(tools.values()),
}
def main():
messages = [
system_info,
{
"role": "user",
"content": "ๅธฎๆˆ‘ๆŸฅ่ฏขๅŒ—ไบฌ็š„ๅคฉๆฐ”ๆ€Žไนˆๆ ท",
}
]
response = openai.ChatCompletion.create(
model="chatglm3",
messages=messages,
temperature=0,
return_function_call=True
)
function_call = json.loads(response.choices[0].message.content)
logger.info(f"Function Call Response: {function_call}")
tool_response = dispatch_tool(function_call["name"], function_call["parameters"])
logger.info(f"Tool Call Response: {tool_response}")
messages = response.choices[0].history # ่Žทๅ–ๅŽ†ๅฒๅฏน่ฏไฟกๆฏ
messages.append(
{
"role": "observation",
"content": tool_response, # ่ฐƒ็”จๅ‡ฝๆ•ฐ่ฟ”ๅ›ž็ป“ๆžœ
}
)
response = openai.ChatCompletion.create(
model="chatglm3",
messages=messages,
temperature=0,
)
logger.info(response.choices[0].message.content)
if __name__ == "__main__":
main()