Package: microchain-python
microchainfunction calling-based LLM agents. Just that, no bloat.Installationpip install microchain-pythonDefine LLM and templatefrommicrochainimportOpenAITextGenerator,HFChatTemplate,LLMgenerator=OpenAITextGenerator(model=MODEL_NAME,api_key=API_KEY,api_base=API_BASE,temperature=0.7)template=HFChatTemplate(CHAT_TEMPLATE)llm=LLM(generator=generator,templates=[template])UseHFChatTemplate(template)to usetokenizer.apply_chat_templatefromhuggingface.You can also useVicunaTemplate()for a classic vicuna-style prompt.To use ChatGPT APIs you don't need to apply a template:frommicrochainimportOpenAIChatGenerator,LLMgenerator=OpenAIChatGenerator(model="gpt-3.5-turbo",api_key=API_KEY,api_base="https://api.openai.com/v1",temperature=0.7)llm=LLM(generator=generator)Define LLM functionsDefineLLM callable functionsas plain Python objects. Usetype annotationsto instruct the LLM to use the correct types.frommicrochainimportFunctionclassSum(Function):@propertydefdescription(self):return"Use this function to compute the sum of two numbers"@propertydefexample_args(self):return[2,2]def__call__(self,a:float,b:float):returna+bclassProduct(Function):@propertydefdescription(self):return"Use this function to compute the product of two numbers"@propertydefexample_args(self):return[2,2]def__call__(self,a:float,b:float):returna*bprint(Sum().help)'''Sum(a: int, b: int)This function sums two numbers.Example: Sum(a=2, b=2)'''print(Product().help)'''Product(a: float, b: float)Use this function to compute the product of two numbers.Example: Product(a=2, b=2)'''Define a LLM AgentRegister your functions with anEngine()using theregister()function.Create anAgent()using thellmand the executionengine.Define a prompt for the LLM and include the functions documentation usingengine.help().It's always a good idea to bootstrap the LLM with examples of function calls. Do this by settingengine.bootstrap = [...]with a list of function calls to run and prepend their results to the chat history.frommicrochainimportAgent,Enginefrommicrochain.functionsimportReasoning,Stopengine=Engine()engine.register(Reasoning())engine.register(Stop())engine.register(Sum())engine.register(Product())agent=Agent(llm=llm,engine=engine)agent.prompt=f"""Act as a calculator. You can use the following functions:{engine.help}Only output valid Python function calls.How much is (2*4 + 3)*5?"""agent.bootstrap=['Reasoning("I need to reason step-by-step")',]agent.run()Running it will output something like:prompt:
Act as a calculator. You can use the following functions:

Reasoning(reasoning: str)
Use this function for your internal reasoning.
Example: Reasoning(reasoning=The next step to take is...)

Stop()
Use this function to stop the program.
Example: Stop()

Sum(a: float, b: float)
Use this function to compute the sum of two numbers.
Example: Sum(a=2, b=2)

Product(a: float, b: float)
Use this function to compute the product of two numbers.
Example: Product(a=2, b=2)


Only output valid Python function calls.

How much is (2*4 + 3)*5?

Running 10 iterations
>> Reasoning("I need to reason step-by-step")
The reasoning has been recorded
>> Reasoning("First, calculate the product of 2 and 4")
The reasoning has been recorded
>> Product(a=2, b=4)
8
>> Reasoning("Then, add 3 to the product of 2 and 4")
The reasoning has been recorded
>> Sum(a=8, b=3)
11
>> Reasoning("Lastly, multiply the sum by 5")
The reasoning has been recorded
>> Product(a=11, b=5)
55
>> Reasoning("So, the result of (2*4 + 3)*5 is 55")
The reasoning has been recorded
>> Stop()
The program has been stoppedYou can find more exampleshere