commit e42a5bfa7c5af75015a305a71d59aaaeb4dac2a8 Author: Jono Targett Date: Sun Jun 25 16:22:22 2023 +0930 Initial commit diff --git a/gpt_sort.py b/gpt_sort.py new file mode 100755 index 0000000..5ba60fe --- /dev/null +++ b/gpt_sort.py @@ -0,0 +1,32 @@ +#! /usr/bin/env python3 + +from langchain.chains import LLMChain +from langchain.llms import OpenAI +from langchain.prompts import PromptTemplate + +llm = OpenAI(temperature=0) + + +def gpt_sort(items: list) -> list: + sort_prompt = PromptTemplate( + input_variables=["unsorted_items"], + template="""{unsorted_items} + + Please sort the items in the list. Format your output as a python list. + """, + ) + + sort_chain = LLMChain(prompt=sort_prompt, llm=llm) + sorted_items = eval(sort_chain.run(items)) + + return sorted_items + + +if __name__ == "__main__": + test_data = [4, 3, 22, -1, 1, 0, False, "banana", 1e100, -99] + + results = gpt_sort(test_data) + + # Output: [False, -99, -1, 1, 3, 4, 22, 'banana', 1e+100] + print(results) + print("Is a list? ", isinstance(results, list)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b63e77a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +langchain +openai