Created
June 30, 2024 07:25
-
-
Save unclecode/e3e13d17e3988bc921df3600fd2557d0 to your computer and use it in GitHub Desktop.
Experimenting with Qwen2 function call for small models of 500 million and 1.5 billion parameters.
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
| import json | |
| from qwen_agent.llm import get_chat_model | |
| # I experimented with the 500 million parameters. It works nicely, but sometimes it doesn't detect all the parameters. For example, in the getCurrentWeather function, it may not detect the value for the second argument, which is the format of the temperature. So it's good practice to set default values for them. However, for very critical parameters, it understands them well. | |
| llm = get_chat_model({ | |
| # 'model': 'qwen2:0.5b', | |
| 'model': 'qwen2:1.5b', | |
| 'model_server': 'http://localhost:11434/v1', | |
| 'api_key': 'EMPTY', | |
| }) | |
| def execute(llm, messages, functions): | |
| # Create a dictionary mapping function names to function objects | |
| function_dict = {func["name"]: globals()[func["name"]] for func in functions} | |
| while True: | |
| # Call the LLM with the current messages and functions | |
| response = llm.chat(messages=messages, functions=functions, stream=False) | |
| latest_message = response[-1] | |
| # Add the latest message to the messages | |
| messages.append(latest_message) | |
| # Check if the latest message has a function call | |
| if latest_message.get("function_call"): | |
| print("Function Call:", json.dumps(latest_message["function_call"], indent=2)) | |
| function_name = latest_message["function_call"]["name"] | |
| arguments = json.loads(latest_message["function_call"]["arguments"]) | |
| # Execute the function with the provided arguments | |
| function_result = function_dict[function_name](**arguments) | |
| print("Function Result:", json.dumps(function_result, indent=2)) | |
| # Add the function result to the messages | |
| messages.append({ | |
| "role": "function", | |
| "name": function_name, | |
| "content": json.dumps(function_result) | |
| }) | |
| else: | |
| print("Final Response:", json.dumps(response[-1]["content"], indent=2)) | |
| # If there's no function call, return the final response | |
| return response | |
| import time, math | |
| def get_current_weather(location, format ): | |
| """ | |
| Get the current weather, for the given location in the given format | |
| """ | |
| time.sleep(0.5) | |
| return { | |
| 'temperature': 10.0, | |
| 'unit': 'celsius' | |
| } | |
| def get_current_temperature(location): | |
| """ | |
| Get the current temperature, for the given location | |
| """ | |
| time.sleep(0.5) | |
| return { | |
| 'temperature': 10.0, | |
| 'unit': 'celsius' | |
| } | |
| def get_current_time(location): | |
| """ | |
| Get the current time, for the given location | |
| """ | |
| time.sleep(0.5) | |
| return { | |
| 'time': '10:00' | |
| } | |
| def convert_temperature(temperature, from_unit, to_unit): | |
| """ | |
| Convert temperature from one unit to another | |
| """ | |
| time.sleep(0.5) | |
| return { | |
| 'temperature': 10.0, | |
| 'unit': to_unit | |
| } | |
| def sin(value): | |
| """ | |
| Calculate the sine of a number | |
| """ | |
| return { | |
| 'sin': math.sin(value) | |
| } | |
| def cos(value): | |
| """ | |
| Calculate the cosine of a number | |
| """ | |
| return { | |
| 'cos': math.cos(value) | |
| } | |
| def exp(value): | |
| """ | |
| Calculate the exponential of a number | |
| """ | |
| return { | |
| 'exp': math.exp(value) | |
| } | |
| def pow(base, exponent): | |
| """ | |
| Calculate the power of a number | |
| """ | |
| return { | |
| 'pow': math.pow(base, exponent) | |
| } | |
| messages= [ | |
| { | |
| 'role': 'user', | |
| 'content': "What's the weather like today in Paris? Also tell me the time in San Francisco?" | |
| } | |
| ] | |
| functions = [ | |
| { | |
| "name": "get_current_weather", | |
| "description": "Get the current weather", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "location": { | |
| "type": "string", | |
| "description": "The city and state, e.g. San Francisco, CA" | |
| }, | |
| "format": { | |
| "type": "string", | |
| "enum": ["celsius", "fahrenheit"], | |
| "description": "The temperature unit to use. Infer this from the user's location." | |
| } | |
| }, | |
| "required": ["location", "format"] | |
| } | |
| }, | |
| { | |
| "name": "get_current_time", | |
| "description": "Get the current time", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "location": { | |
| "type": "string", | |
| "description": "The city and state, e.g. San Francisco, CA" | |
| } | |
| }, | |
| "required": ["location"] | |
| } | |
| } | |
| ] | |
| final_response = execute(llm, messages, functions) | |
| """ | |
| Function Call: { | |
| "name": "get_current_weather", | |
| "arguments": "{\n \"location\": \"Paris\"\n}" | |
| } | |
| Function Result: { | |
| "temperature": 10.0, | |
| "unit": "celsius" | |
| } | |
| Final Response: "Today's weather in Paris is a sunny and mild day with an average temperature of 10 degrees Celsius.\n\nIn San Francisco, the current time is:\n\n4:30 PM. | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment