from pathlib import Path import sys sys.path.append(str(Path(__file__).resolve().parents[1])) from _chapter2_config import as_text, get_chat_model, print_dependency_hint try: from langchain_core.prompts import ChatPromptTemplate except ModuleNotFoundError as exc: print_dependency_hint(exc) raise SystemExit(1) def main() -> None: try: model = get_chat_model(temperature=0) except ModuleNotFoundError as exc: print_dependency_hint(exc) raise SystemExit(1) template_str = "帮我讲个关于{name}笑话吧" prompt_template = ChatPromptTemplate.from_template(template_str) prompt = prompt_template.format_messages(name="气球") print(f"prompt-->{prompt}") result = model.invoke(prompt) print(f"result-->{as_text(result)}") if __name__ == "__main__": main()