OoriData / OgbujiPT

Client-side toolkit for using large language models, including where self-hosted
Apache License 2.0
101 stars 8 forks source link

Where is the support for Functions? #50

Closed stevechappell2000 closed 9 months ago

stevechappell2000 commented 10 months ago

I can nothing documenting how to upgrade or how to use the functions array in the new chat.completions call.

uogbuji commented 10 months ago

Hi, sorry, we're pretty heads down trying to get the 0.6.0 release out, and I haven't had a chance to catch up on tickets.

Function calling should work transparently. We don't really provide any particular, fancy features, but below, for example is a simple script using it. It's based on an old script I had lying around on my drive, based on some example I'd found online at some point. You probably need to run it using the 49-pythonopenai-breaking branch, for now, given OpenAI's breaking changes from Monday's Dev Day. As soon as we release 0.6.0 it should just work.

import os
import json
from typing import List
from pydantic import BaseModel
from ogbujipt.llm_wrapper import openai_chat_api, prompt_to_chat

# PyDantic schema for function spec output
class StepByStepAIResponse(BaseModel):
    title: str
    steps: List[str]

llm_api = openai_chat_api(model='gpt-4')

messages=[
       {"role": "user", "content": "Explain how to assemble a PC"}
    ]

functions=[
        {
          "name": "get_answer_for_user_query",
          "description": "Get user answer in series of steps",
          "parameters": StepByStepAIResponse.schema()
        }
]

function_call={"name": "get_answer_for_user_query"}

resp = llm_api(messages=messages, functions=functions, function_call=function_call)
# resp.choices[0].message.function_call
# resp.choices[0].message.function_call.arguments
print(resp.choices[0].message.function_call.arguments)

Output:

{
  "title": "Assembling a PC",
  "steps": [
    "Purchase all the necessary parts such as the computer case, motherboard, CPU, GPU, RAM, power supply, hard drive or SSD, and any additional components like a cooling system or optical drives.",
    "Begin by preparing your case. Remove the side panels, and remove any pre-installed fans or brackets that might conflict with your motherboard or other components.",
    "Install the power supply (PSU). Secure it into its position with the included screws.",
    "Insert the CPU into the motherboard’s CPU socket, then install the cooler on top of it. Be sure to apply thermal paste between the cooler and CPU if it's not pre-applied.",
    "Insert the RAM into the appropriate slots on your motherboard.",
    "Secure the motherboard into the case with the included screws.",
    "Install the GPU onto the PCIe x16 slot (usually the top one) of the motherboard.",
    "Connect the power cables from your power supply to your motherboard, GPU, and any storage drives you have.",
    "Connect your hard drive or SSD. If you're using an SSD, you might need to screw it into a 2.5” drive bay. Connect the data and power cables for each drive.",
    "Install any additional components like optical drives, and connect them to the power supply and motherboard accordingly.",
    "Connect the front panel buttons and lights to the appropriate pins on your motherboard.",
    "Secure the side panels back onto your case.",
    "Recheck all connections, then connect a monitor, keyboard, and mouse. Turn on the PC to ensure everything works correctly. If it boots up successfully, you can proceed with installing your operating system.",
    "After the installation of the operating system, install the necessary drivers and perform a systems check to ensure all hardware is functioning as expected."
  ]
}
choccccy commented 9 months ago

howdy @stevechappell2000! I think @uogbuji has demonstrated that OgbujiPT is natively able to utilize function calls. We definitely should have a demo and more documentation on the subject though.

Feel free to reopen this ticket if you feel as though this wasn't the solution you're looking for!

uogbuji commented 9 months ago

@stevechappell2000 I added a demo for function calling. If you weren't sure how to do that, probably true for others. Thanks for raising the issue!

@choccccy, what do you think? I just left this demo as a simple script, rather than a command-line program (e.g. using click). I think that' probably OK since we have plenty of examples of the latter. We'll want to be adding more demos, and reducing the friction to doing so makes sense to me, but wanted to see what you think.

choccccy commented 9 months ago

Yeah, makes sense. Making/polishing/fixing demos always seems to get shoved to the back burner, so minimizing friction seems like the right call.