|
| 1 | +""" |
| 2 | +(deprecated) Experiment script for OSS models using Ollama |
| 3 | +This script reproduce legacy results for OSS models using Ollama in our paper's Appendix. |
| 4 | +New models should use our vLLM option instead. |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Union |
| 8 | +import os |
| 9 | +import json |
| 10 | + |
| 11 | +from ollama import Client as OllamaClient, ResponseError |
| 12 | +import fire |
| 13 | +from dacite import from_dict |
| 14 | +from tqdm import tqdm |
| 15 | +from funcy_chain import Chain |
| 16 | + |
| 17 | +from tfbench.lm import get_sys_prompt |
| 18 | +from tfbench.common import BenchmarkTask, get_prompt |
| 19 | +from tfbench.postprocessing import postprocess, RESPONSE_STRATEGIES |
| 20 | +from tfbench.evaluation import evaluate |
| 21 | + |
| 22 | +OLLAMA_OSS = [ |
| 23 | + "phi3:3.8b", |
| 24 | + "phi3:14b", |
| 25 | + "mistral", |
| 26 | + "mixtral:8x7b", |
| 27 | + "mixtral:8x22b", |
| 28 | + "llama3:8b", |
| 29 | + "llama3:70b", |
| 30 | + "llama3.1:8b", |
| 31 | + "llama3.1:70b", |
| 32 | + "llama3.1:405b", |
| 33 | + "llama3.2:1b", |
| 34 | + "llama3.2:3b", |
| 35 | + "llama3.3:70b", |
| 36 | + "gemma:2b", |
| 37 | + "gemma:7b", |
| 38 | + "gemma2:9b", |
| 39 | + "gemma2:27b", |
| 40 | + "qwen2:1.5b", |
| 41 | + "qwen2:7b", |
| 42 | + "qwen2:72b", |
| 43 | + "qwen2.5:1.5b", |
| 44 | + "qwen2.5:7b", |
| 45 | + "qwen2.5:72b", |
| 46 | + "deepseek-v2:16b", |
| 47 | + "deepseek-v2:236b", |
| 48 | + "deepseek-v2.5:236b", |
| 49 | +] |
| 50 | + |
| 51 | + |
| 52 | +OLLAMA_CODE = [ |
| 53 | + "qwen2.5-coder:1.5b", |
| 54 | + "qwen2.5-coder:7b", |
| 55 | + "granite-code:3b", |
| 56 | + "granite-code:8b", |
| 57 | + "granite-code:20b", |
| 58 | + "granite-code:34b", |
| 59 | + "deepseek-coder-v2:16b", |
| 60 | + "deepseek-coder-v2:236b", |
| 61 | +] |
| 62 | + |
| 63 | +OLLAMA_MODELS = OLLAMA_OSS + OLLAMA_CODE |
| 64 | + |
| 65 | + |
| 66 | +def get_ollama_model( |
| 67 | + client: OllamaClient, |
| 68 | + model: str = "llama3:8b", |
| 69 | + pure: bool = False, |
| 70 | +): |
| 71 | + """ |
| 72 | + Configure and return a function to generate type signatures using an Ollama model. |
| 73 | +
|
| 74 | + Parameters: |
| 75 | + client (OllamaClient): The Ollama client instance used for sending requests to the model. |
| 76 | +
|
| 77 | + model (str): Name of the model to use for generating type signatures. |
| 78 | + Must be one of the predefined models in OLLAMA_MODELS. |
| 79 | + Default is "llama3:8b". |
| 80 | +
|
| 81 | + pure (bool): If True, uses the original variable naming in type inference. |
| 82 | + If False, uses rewritten variable naming (e.g., `v1`, `v2`, ...). Default is False. |
| 83 | +
|
| 84 | + Returns: |
| 85 | + Callable[[str], Union[str, None]]: |
| 86 | + A function that takes a prompt string as input and returns the generated type |
| 87 | + signature as a string, or None if the generation fails. |
| 88 | + """ |
| 89 | + |
| 90 | + def generate_type_signature(prompt: str) -> Union[str, None]: |
| 91 | + try: |
| 92 | + response = client.chat( |
| 93 | + messages=[ |
| 94 | + { |
| 95 | + "role": "system", |
| 96 | + "content": get_sys_prompt(pure), |
| 97 | + }, |
| 98 | + {"role": "user", "content": prompt}, |
| 99 | + ], |
| 100 | + model=model, |
| 101 | + ) |
| 102 | + except ResponseError as e: |
| 103 | + print(e) |
| 104 | + return None |
| 105 | + |
| 106 | + message = response.message |
| 107 | + if message.content: |
| 108 | + return str(message.content) |
| 109 | + |
| 110 | + return None |
| 111 | + |
| 112 | + return generate_type_signature |
| 113 | + |
| 114 | + |
| 115 | +def main( |
| 116 | + model: str = "llama3:8b", |
| 117 | + pure: bool = False, |
| 118 | + port: int = 11434, |
| 119 | + output_file: str | None = None, |
| 120 | + log_file: str = "evaluation_log.jsonl", |
| 121 | +): |
| 122 | + """ |
| 123 | + Run an experiment using various AI models to generate and evaluate type signatures. |
| 124 | +
|
| 125 | + Parameters: |
| 126 | + model (str): Name of the model to use for generating type signatures. Must be one of OLLAMA_MODELS |
| 127 | +
|
| 128 | + port (int): Port number for connecting to the Ollama server. |
| 129 | + Ignored for other models. Default is 11434. |
| 130 | +
|
| 131 | + pure (bool): If True, uses the original variable naming in type inference. |
| 132 | + If False, uses rewritten variable naming (e.g., `v1`, `v2`, ...). Default is False. |
| 133 | + """ |
| 134 | + assert model in OLLAMA_MODELS, f"{model} is not supported." |
| 135 | + |
| 136 | + # hard-coding benchmark file path for experiment |
| 137 | + input_file = "tfb.pure.json" if pure else "tfb.json" |
| 138 | + input_file = os.path.abspath(input_file) |
| 139 | + assert os.path.exists( |
| 140 | + input_file |
| 141 | + ), f"{input_file} does not exist! Please download or build it first." |
| 142 | + |
| 143 | + if output_file is None: |
| 144 | + os.makedirs("result", exist_ok=True) |
| 145 | + output_file = f"result/{model}.txt" |
| 146 | + |
| 147 | + client = OllamaClient(host=f"http://localhost:{port}") |
| 148 | + generate = get_ollama_model(client, model, pure) |
| 149 | + |
| 150 | + with open(input_file, "r") as fp: |
| 151 | + tasks = [from_dict(data_class=BenchmarkTask, data=d) for d in json.load(fp)] |
| 152 | + |
| 153 | + prompts = map(get_prompt, tasks) |
| 154 | + responses = map(generate, tqdm(prompts, desc=model)) |
| 155 | + gen_results = ( |
| 156 | + Chain(responses) |
| 157 | + .map(lambda x: x if x is not None else "") # convert None to empty string |
| 158 | + .map(lambda s: postprocess(s, RESPONSE_STRATEGIES)) |
| 159 | + .map(str.strip) |
| 160 | + .value |
| 161 | + ) |
| 162 | + |
| 163 | + with open(output_file, "w", errors="ignore") as file: |
| 164 | + file.write("\n".join(gen_results)) |
| 165 | + |
| 166 | + eval_acc = evaluate(tasks, gen_results) |
| 167 | + print(eval_acc) |
| 168 | + |
| 169 | + os.makedirs(os.path.dirname(output_file), exist_ok=True) |
| 170 | + with open(log_file, "a") as fp: |
| 171 | + logging_result = {"model_name": model, **eval_acc, "pure": pure} |
| 172 | + fp.write(f"{json.dumps(logging_result)}\n") |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + fire.Fire(main) |
0 commit comments