From 2797d903805cddc135bb8392a2de563ac8855d1b Mon Sep 17 00:00:00 2001 From: MIDORIBIN Date: Sun, 23 Jul 2023 10:20:03 +0900 Subject: [PATCH] add: sample --- README.md | 2 ++ sample.py => sample/llm_sample.py | 4 ++-- sample/prompt_template_sample.py | 27 ++++++++++++++++++++++++ sample/sequential_chain_sample.py | 35 +++++++++++++++++++++++++++++++ sample/simple_chain_sample.py | 23 ++++++++++++++++++++ 5 files changed, 89 insertions(+), 2 deletions(-) rename sample.py => sample/llm_sample.py (84%) create mode 100644 sample/prompt_template_sample.py create mode 100644 sample/sequential_chain_sample.py create mode 100644 sample/simple_chain_sample.py diff --git a/README.md b/README.md index 453b42a..c930b18 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ if __name__ == '__main__': The above sample code demonstrates the basic usage of langchain_g4f. Choose the appropriate model and provider, initialize the LLM, and then pass input text to the LLM object to obtain the result. +For other samples, please refer to the following [sample directory](./sample/). + ## Support and Bug Reports For support and bug reports, please use the GitHub Issues page. diff --git a/sample.py b/sample/llm_sample.py similarity index 84% rename from sample.py rename to sample/llm_sample.py index a1a5314..804d7bb 100644 --- a/sample.py +++ b/sample/llm_sample.py @@ -10,9 +10,9 @@ def main(): provider=Provider.Aichat, ) - res = llm('hello') + res = llm("hello") print(res) # Hello! How can I assist you today? -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/sample/prompt_template_sample.py b/sample/prompt_template_sample.py new file mode 100644 index 0000000..b45b087 --- /dev/null +++ b/sample/prompt_template_sample.py @@ -0,0 +1,27 @@ +from g4f import Provider, Model +from langchain.llms.base import LLM +from langchain import PromptTemplate + +from langchain_g4f import G4FLLM + + +def main(): + template = "What color is the {fruit}?" + prompt_template = PromptTemplate(template=template, input_variables=["fruit"]) + + llm: LLM = G4FLLM( + model=Model.gpt_35_turbo, + provider=Provider.Aichat, + ) + + res = llm(prompt_template.format(fruit="apple")) + print(res) + # The color of an apple can vary, but it is typically red, green, or yellow. + + res = llm(prompt_template.format(fruit="lemon")) + print(res) + # The color of a lemon is typically yellow. + + +if __name__ == "__main__": + main() diff --git a/sample/sequential_chain_sample.py b/sample/sequential_chain_sample.py new file mode 100644 index 0000000..356ae0d --- /dev/null +++ b/sample/sequential_chain_sample.py @@ -0,0 +1,35 @@ +from g4f import Provider, Model +from langchain.llms.base import LLM +from langchain import PromptTemplate +from langchain.chains import LLMChain, SimpleSequentialChain + +from langchain_g4f import G4FLLM + + +def main(): + llm: LLM = G4FLLM( + model=Model.gpt_35_turbo, + provider=Provider.DeepAi, + ) + + prompt_template_1 = PromptTemplate( + input_variables=["location"], + template="Please tell us one tourist attraction in {location}.", + ) + chain_1 = LLMChain(llm=llm, prompt=prompt_template_1) + + prompt_template_2 = PromptTemplate( + input_variables=["location"], + template="What is the train route from Tokyo Station to {location}?", + ) + chain_2 = LLMChain(llm=llm, prompt=prompt_template_2) + + simple_sequential_chain = SimpleSequentialChain( + chains=[chain_1, chain_2], verbose=True + ) + + print(simple_sequential_chain("tokyo")) + + +if __name__ == "__main__": + main() diff --git a/sample/simple_chain_sample.py b/sample/simple_chain_sample.py new file mode 100644 index 0000000..d5b289b --- /dev/null +++ b/sample/simple_chain_sample.py @@ -0,0 +1,23 @@ +from g4f import Provider, Model +from langchain.llms.base import LLM +from langchain import PromptTemplate +from langchain.chains import LLMChain + +from langchain_g4f import G4FLLM + + +def main(): + llm: LLM = G4FLLM( + model=Model.gpt_35_turbo, + provider=Provider.Aichat, + ) + prompt_template = PromptTemplate( + input_variables=["location"], + template="Where is the best tourist attraction in {location}?", + ) + chain = LLMChain(llm=llm, prompt=prompt_template) + print(chain("tokyo")) + + +if __name__ == "__main__": + main()