mirror of
https://github.com/tiennm99/litellm.git
synced 2026-08-01 12:22:11 +00:00
8.7 KiB
8.7 KiB
In [1]:
from langchain import PromptTemplate, OpenAI, LLMChain
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0), verbose=True)In [2]:
llm_chain.save("llm_chain.json")In [3]:
!cat llm_chain.json{
"memory": null,
"verbose": true,
"prompt": {
"input_variables": [
"question"
],
"output_parser": null,
"template": "Question: {question}\n\nAnswer: Let's think step by step.",
"template_format": "f-string"
},
"llm": {
"model_name": "text-davinci-003",
"temperature": 0.0,
"max_tokens": 256,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"n": 1,
"best_of": 1,
"request_timeout": null,
"logit_bias": {},
"_type": "openai"
},
"output_key": "text",
"_type": "llm_chain"
}In [4]:
from langchain.chains import load_chainIn [5]:
chain = load_chain("llm_chain.json")In [6]:
chain.run("whats 2 + 2")Out [6]:
[1m> Entering new LLMChain chain...[0m Prompt after formatting: [32;1m[1;3mQuestion: whats 2 + 2 Answer: Let's think step by step.[0m [1m> Finished chain.[0m
' 2 + 2 = 4'
In [7]:
llm_chain.prompt.save("prompt.json")In [8]:
!cat prompt.json{
"input_variables": [
"question"
],
"output_parser": null,
"template": "Question: {question}\n\nAnswer: Let's think step by step.",
"template_format": "f-string"
}In [9]:
llm_chain.llm.save("llm.json")In [10]:
!cat llm.json{
"model_name": "text-davinci-003",
"temperature": 0.0,
"max_tokens": 256,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"n": 1,
"best_of": 1,
"request_timeout": null,
"logit_bias": {},
"_type": "openai"
}In [11]:
config = {
"memory": None,
"verbose": True,
"prompt_path": "prompt.json",
"llm_path": "llm.json",
"output_key": "text",
"_type": "llm_chain",
}
import json
with open("llm_chain_separate.json", "w") as f:
json.dump(config, f, indent=2)In [12]:
!cat llm_chain_separate.json{
"memory": null,
"verbose": true,
"prompt_path": "prompt.json",
"llm_path": "llm.json",
"output_key": "text",
"_type": "llm_chain"
}In [13]:
chain = load_chain("llm_chain_separate.json")In [15]:
chain.run("whats 2 + 2")Out [15]:
[1m> Entering new LLMChain chain...[0m Prompt after formatting: [32;1m[1;3mQuestion: whats 2 + 2 Answer: Let's think step by step.[0m [1m> Finished chain.[0m
' 2 + 2 = 4'
In [ ]: