Package: llmfoo
LLM FOOOverviewLLM FOO is a cutting-edge project blending the art of Kung Fu with the science of Large Language Models... or
actually this is about automatically making the OpenAI tool JSON Schema, parsing call and constructing the
result to the chat model.
And then there is a second utilityis_statement_truethat usesgenius logit_bias trickthat only uses one output token.But hey I hope this will become a set of small useful LLM helper functions that will make building stuff easier
because current bleeding edge APIs are a bit of a mess and I think we can do better.InstallationpipinstallllmfooUsageYou need to have OPENAI_API_KEY in env and ability to callgpt-4-1106-previewmodelis_statement_trueshould be easy to understand.
Make some natural language statement, and check it against criteria or general truthfulness. You get back boolean.For the LLM FOO tool:Add@toolannotation.llmfoo will generate the json schema to YOURFILE.tool.json with GPT-4-Turbo - "Never send a machine to do a human's job" .. like who wants to write boilerplate docs for Machines???Annotated functions have helpers:openai_schemato return the schema (You can edit it from the json if your not happy with what the machines did)openai_tool_callto make the tool call and return the result in chat API message formatopenai_tool_outputto make the tool call and return the result in assistant API tool output formatfromtimeimportsleepfromopenaiimportOpenAIfromllmfoo.functionsimporttoolfromllmfooimportis_statement_truedeftest_is_statement_true_with_default_criteria():assertis_statement_true("Earth is a planet.")assertnotis_statement_true("1 + 2 = 5")deftest_is_statement_true_with_own_criteria():assertnotis_statement_true("Temperature outside is -2 degrees celsius",criteria="Temperature above 0 degrees celsius")assertis_statement_true("1984 was written by George Orwell",criteria="George Orwell is the author of 1984")deftest_is_statement_true_criteria_can_change_truth_value():assertis_statement_true("Earth is 3rd planet from the Sun")assertnotis_statement_true("Earth is 3rd planet from the Sun",criteria="Earth is stated to be 5th planet from the Sun")@tooldefadder(x:int,y:int)->int:returnx+y@tooldefmultiplier(x:int,y:int)->int:returnx*yclient=OpenAI()deftest_chat_completion_with_adder():number1=3267182746number2=798472847messages=[{"role":"user","content":f"What is{number1}+{number2}?"}]response=client.chat.completions.create(model="gpt-4-1106-preview",messages=messages,tools=[adder.openai_schema])messages.append(response.choices[0].message)messages.append(adder.openai_tool_call(response.choices[0].message.tool_calls[0]))response2=client.chat.completions.create(model="gpt-4-1106-preview",messages=messages,tools=[adder.openai_schema])assertstr(adder(number1,number2))inresponse2.choices[0].message.content.replace(",","")deftest_assistant_with_multiplier():number1=1238763428176number2=172388743612assistant=client.beta.assistants.create(name="The Calc Machina",instructions="You are a calculator with a funny pirate accent.",tools=[multiplier.openai_schema],model="gpt-4-1106-preview")thread=client.beta.threads.create(messages=[{"role":"user","content":f"What is{number1}*{number2}?"}])run=client.beta.threads.runs.create(thread_id=thread.id,assistant_id=assistant.id)whileTrue:run_state=client.beta.threads.runs.retrieve(run_id=run.id,thread_id=thread.id,)ifrun_state.statusnotin['in_progress','requires_action']:breakifrun_state.status=='requires_action':tool_call=run_state.required_action.submit_tool_outputs.tool_calls[0]run=client.beta.threads.runs.submit_tool_outputs(thread_id=thread.id,run_id=run.id,tool_outputs=[multiplier.openai_tool_output(tool_call)])sleep(1)sleep(0.1)messages=client.beta.threads.messages.list(thread_id=thread.id)assertstr(multiplier(number1,number2))inmessages.data[0].content[0].text.value.replace(",","")ContributingInterested in contributing? Loved to get your help to make this project better!
The APIs under are changing and system is still very much first version.LicenseThis project is licensed under theMIT License.AcknowledgementsThanks to all the contributors and maintainers.Special thanks to the Kung Fu masters such as Bruce Lee who inspired this project.