| |
| |
|
|
| """Implementation Berkeley Function Calling Leaderboard evals using https://github.com/ShishirPatil/gorilla/blob/main/berkeley-function-call-leaderboard/README.md |
| """ |
| import copy |
| import json |
| import pdb |
| import textwrap |
| from collections import defaultdict |
| from functools import partial |
| from typing import Any, DefaultDict, Dict, List |
| from datasets import load_dataset, DatasetDict, Dataset |
| import pandas as pd |
|
|
|
|
| |
| AST_NON_LIVE_CATEGORY = [ |
| "simple", |
| "multiple", |
| "parallel", |
| "parallel_multiple", |
| "java", |
| "javascript", |
| ] |
|
|
| |
| AST_LIVE_CATEGORY = [ |
| "live_simple", |
| "live_multiple", |
| "live_parallel", |
| "live_parallel_multiple", |
| ] |
| RELEVANCE_CATEGORY = ["irrelevance", "live_relevance", "live_irrelevance"] |
|
|
| |
| DEFAULT_SYSTEM_PROMPT_WITHOUT_FUNC_DOC = """ |
| You are an expert in composing functions. You are given a question and a set of possible functions. |
| Based on the question, you will need to make one or more function/tool calls to achieve the purpose. |
| If none of the function can be used, point it out. If the given question lacks the parameters required by the function, |
| also point it out. You should only return the function call in tools call sections. |
| |
| If you decide to invoke any of the function(s), you MUST put it in the format of [func_name1(params_name1=params_value1, params_name2=params_value2...), func_name2(params)]\n |
| You SHOULD NOT include any other text in the response. |
| """ |
|
|
| DEFAULT_SYSTEM_PROMPT = ( |
| DEFAULT_SYSTEM_PROMPT_WITHOUT_FUNC_DOC |
| + """ |
| Here is a list of functions in JSON format that you can invoke.\n\n{functions}\n |
| """ |
| ) |
|
|
| USER_PROMPT_TEMPLATE = textwrap.dedent( |
| """{{ question }} |
| """ |
| ) |
| SYS_PROMPT_TEMPLATE = """\n\n{{ sys_prompt }}\n\n""" |
|
|
|
|
| def func_doc_language_specific_pre_processing( |
| function: List[Dict[str, Any]], test_category: str |
| ) -> List[Dict[str, Any]]: |
| def _get_language_specific_hint(test_category): |
| if test_category == "java": |
| return " Note that the provided function is in Java 8 SDK syntax." |
| elif test_category == "javascript": |
| return " Note that the provided function is in JavaScript syntax." |
| else: |
| return " Note that the provided function is in Python 3 syntax." |
|
|
| if len(function) == 0: |
| return function |
| assert type(function) == list |
| for item in function: |
| |
| func_description = item["description"] |
| item["description"] = item["description"] + _get_language_specific_hint( |
| test_category |
| ) |
| |
| properties = item["parameters"]["properties"] |
| if test_category == "java": |
| for key, value in properties.items(): |
| if value["type"] == "any": |
| properties[key][ |
| "description" |
| ] += " This parameter can be of any type of Java object in string representation." |
| else: |
| value[ |
| "description" |
| ] += f" This is Java {value['type']} type parameter in string representation." |
| if value["type"] == "ArrayList" or value["type"] == "Array": |
| value[ |
| "description" |
| ] += f" The list elements are of type {value['items']['type']}; they are not in string representation." |
| del value["items"] |
|
|
| value["type"] = "string" |
|
|
| elif test_category == "javascript": |
| for key, value in properties.items(): |
| if value["type"] == "any": |
| properties[key][ |
| "description" |
| ] += " This parameter can be of any type of JavaScript object in string representation." |
| else: |
| value[ |
| "description" |
| ] += f" This is JavaScript {value['type']} type parameter in string representation." |
| if value["type"] == "array": |
| value[ |
| "description" |
| ] += f" The list elements are of type {value['items']['type']}; they are not in string representation." |
| del value["items"] |
|
|
| if value["type"] == "dict": |
| if "properties" in value: |
| value[ |
| "description" |
| ] += f" The dictionary entries have the following schema; they are not in string representation. {json.dumps(value['properties'])}" |
| del value["properties"] |
|
|
| value["type"] = "string" |
|
|
| return function |
|
|
|
|
| def preprocess(x: Dict[str, Any]) -> Dict[str, Any]: |
| language = "Python" |
| test_category = "" |
| if "javascript" in x["id"]: |
| language = "JavaScript" |
| test_category = "javascript" |
| elif "java" in x["id"]: |
| language = "Java" |
| test_category = "java" |
| input_functions = copy.deepcopy(x["function"]) |
| func_w_docs = func_doc_language_specific_pre_processing( |
| input_functions, test_category |
| ) |
| system_prompt = DEFAULT_SYSTEM_PROMPT.format(functions=func_w_docs) |
| prompts = x["question"][0] |
| |
| |
| if prompts[0]["role"] == "system": |
| prompts[0]["content"] = system_prompt + "\n\n" + prompts[0]["content"] |
| |
| else: |
| prompts.insert( |
| 0, |
| {"role": "system", "content": system_prompt}, |
| ) |
| question = prompts[1:][0]["content"] |
|
|
| return { |
| "question": question, |
| "sys_prompt": prompts[0]["content"], |
| "language": language, |
| } |
|
|
|
|
|
|
| |
| all_rows = [] |
| def load_jslines(fname): |
| print(f"parsing {fname}") |
| lines = [l for l in open(fname).readlines()] |
| result = [] |
| for l in lines: |
| |
| j = json.loads(l) |
| result.append(j) |
| return result |
|
|
| import os |
| from jinja2 import Environment, Template |
| env = Environment() |
| user_template = env.from_string(USER_PROMPT_TEMPLATE) |
| sys_template = env.from_string(SYS_PROMPT_TEMPLATE) |
|
|
|
|
| cnt = 0 |
| for subset in RELEVANCE_CATEGORY + AST_LIVE_CATEGORY + AST_NON_LIVE_CATEGORY : |
| js = load_jslines(f'data/BFCL_v3_{subset}.json') |
| answer_map = {} |
| if os.path.exists(f'data/possible_answer/BFCL_v3_{subset}.json'): |
| answer_js = load_jslines(f'data/possible_answer/BFCL_v3_{subset}.json') |
| for row in answer_js: |
| answer_map[row['id']] = row['ground_truth'] |
|
|
| for row in js: |
| preprocessed = preprocess(row) |
| row['chat_completion_input'] = json.dumps([ |
| {"role" : "user", |
| "content": user_template.render(**preprocessed) |
| }, |
| {"role": "system", |
| "content": sys_template.render(**preprocessed) |
| } |
| ]) |
| |
| if row['id'] in answer_map: |
| row['ground_truth'] = json.dumps(answer_map[row['id']]) |
| elif 'ground_truth' in row: |
| row['ground_truth'] = json.dumps(row['ground_truth']) |
| row['language'] = preprocessed['language'] |
| row['function'] = json.dumps(row['function']) |
|
|
| all_rows += js |
| |
| |
|
|
| output_df = pd.DataFrame(all_rows) |
|
|
| ds = Dataset.from_pandas(output_df) |
| converted_ds = DatasetDict() |
| converted_ds['train']= ds |
| converted_ds.push_to_hub('teddyyyy123/bfcl_v3') |
| |
| |