33 lines
815 B
Python
Executable File
33 lines
815 B
Python
Executable File
#! /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))
|