from langchain_core.prompts import StringPromptTemplate
def hello_world(abc):
print("Hello, world!")
return abc
PROMPT = """\
你是一个非常有经验和天赋的程序员,现在给你如下函数名称,你会按照如下格式,输出这段代码的名称、源代码、中文解释。
函数名称: {function_name}
源代码:
{source_code}
代码解释:
"""
import inspect
def get_source_code(function_name):
return inspect.getsource(function_name)
class CustomPromptTemplate(StringPromptTemplate):
def format(self, **kwargs) -> str:
function_name = kwargs.get("function_name")
source_code = get_source_code(function_name)
return PROMPT.format(function_name=function_name, source_code=source_code)
a = CustomPromptTemplate(input_variables=["function_name"])
pm = a.format(function_name=hello_world)
print(pm)
from langchain_openai import OpenAI
llm = OpenAI(
model="gpt-3.5-turbo-instruct",
temperature=0,
)
msg = llm.invoke(pm)
print(msg)