pig.llm
This module contains components related to large language model (LLM) interactions within the PIG system. It includes functionalities for constructing prompts, querying LLMs, and processing their responses to facilitate API migration tasks.
prompts
Prompts for LLM queries in the PIG model.
- pig.llm.prompts.generate_prompt(libo, libn)
Generates a prompt for migrating code between libraries.
- Parameters:
libo (str) – A name of the original library.
libn (str) – A name of the new library.
- Returns:
The generated prompt.
- Return type:
str
- pig.llm.prompts.generate_query(libo, libn, apio, cand_apis, codeb)
Generates a query for migrating code between libraries.
- Parameters:
libo (str) – A name of the original library.
libn (str) – A name of the new library.
apio (str) – A name of the original API to be replaced.
cand_apis (str) – A formatted string of candidate APIs.
codeb (str) – The code snippet to be migrated.
- Returns:
The generated query.
- Return type:
str
query
Module for querying the LLM with specific prompts and parameters.
- pig.llm.query.ask_llm(libo, libn, apio, apins, codeb, model, engine, temperature=1)
Queries the LLM with the constructed prompt and returns the response.
- Parameters:
libo (str) – A name of the original library.
libn (str) – A name of the new library.
apio (str) – A name of the original API to be replaced.
apins (list[tuple[str, str]]) – A list of candidate APIs for replacement.
codeb (str) – The code snippet to be migrated.
model (str) – A name of the LLM model to use.
engine (str) – An engine to use (‘openai’ or ‘ollama’).
temperature (Union[float, int], optional) – A value that controls the randomness of the output. Defaults to 1.
- Raises:
ValueError – If an unsupported engine is specified.
- Returns:
The generated query.
- Return type:
str
utils
LLM-related utility functions. This includes functions for extracting relevant information from LLM responses.
- pig.llm.utils.extract_code(llm_answer, libo, libn)
Extracts the migrated code from the LLM answer.
- Parameters:
llm_answer (str) – A response from the LLM.
libo (str) – A name of the original library.
libn (str) – A name of the new library.
- Raises:
ValueError – If the extracted code is invalid or if multiple code blocks are found.
- Returns:
The extracted code.
- Return type:
str
- pig.llm.utils.fill_placeholders(block)
Fills placeholders in the code block with dummy values.
- Parameters:
block (str) – The code block containing placeholders.
- Returns:
The code block with placeholders filled.
- Return type:
str
- pig.llm.utils.find_code_blocks(answer)
Finds all the code blocks from the LLM answer.
- Parameters:
answer (str) – A response from the LLM.
- Returns:
A list of code blocks extracted from the answer.
- Return type:
list[str]
- pig.llm.utils.is_valid_python_code(code)
Checks if the provided code is valid Python code.
- Parameters:
code (str) – The code snippet to be checked.
- Returns:
True if the code is valid Python code, False otherwise.
- Return type:
bool
- pig.llm.utils.show_llm_response(response)
Displays the LLM response for debugging purposes.
- Parameters:
response (str) – A response from the LLM.
- Returns:
None
- Return type:
None
- pig.llm.utils.show_prompt_and_query(prompt, query)
Displays the prompt and query sent to the LLM for debugging purposes.
- Parameters:
prompt (str) – A prompt for the LLM.
query (str) – A query for the LLM.
- Returns:
None
- Return type:
None