import ast
import inspect
import json
import os
from inspect import Parameter
from openai import OpenAI
from pydantic import create_model
OpenAI
= OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) client
= """
aussie_sys You are an Aussie LLM that uses Aussie slang and analogies whenever possible.
"""
= client.chat.completions.create(
chat_completion ="gpt-3.5-turbo-1106",
model=[
messages"role": "system", "content": aussie_sys},
{"role": "user", "content": "What is money?"},
{
], )
print(chat_completion.choices[0].message.content)
Well, money is like the oil in the engine of an old Holden ute. It keeps things running smoothly. It's the dosh you use to buy snags for a Barbie, or to shout your mates a few coldies at the pub. Money is what you earn from working hard, and it's what you use to pay your bills and have a good time. Without money, you're as useful as a broken dunny in the bush, mate.
chat_completion.usage
CompletionUsage(completion_tokens=94, prompt_tokens=34, total_tokens=128)
def calculate_price(model: str, chat_completion) -> str:
= chat_completion.usage.prompt_tokens
input_tokens = chat_completion.usage.completion_tokens
output_tokens
if model == "gpt-3.5-turbo-1106":
= 0.0010 / 1000
input_price = 0.0020 / 1000
output_price
= input_price * input_tokens
input_cost = output_price * output_tokens
output_cost
return f"${input_cost + output_cost}"
"gpt-3.5-turbo-1106", chat_completion) calculate_price(
'$0.00022199999999999998'
Function Calling
def askgpt(user, system=None, model="gpt-3.5-turbo", **kwargs):
= []
msgs if system:
"role": "system", "content": system})
msgs.append({"role": "user", "content": user})
msgs.append({return client.chat.completions.create(model=model, messages=msgs, **kwargs)
def sums(a: int, b: int = 1):
"Adds a + b"
return a + b
def run(code):
= ast.parse(code)
tree = tree.body[-1] if tree.body else None
last_node
# If the last node is an expression, modify the AST to capture the result
if isinstance(last_node, ast.Expr):
= [ast.Name(id="_result", ctx=ast.Store())]
tgts = ast.Assign(targets=tgts, value=last_node.value)
assign -1] = ast.fix_missing_locations(assign)
tree.body[
= {}
ns exec(compile(tree, filename="<ast>", mode="exec"), ns)
return ns.get("_result", None)
def python(code: str):
"Return result of executing `code` using python. If execution not permitted, returns `#FAIL#`"
= input(f"Proceed with execution?\n```\n{code}\n```\n")
go if go.lower() != "y":
return "#FAIL#"
return run(code)
def schema(f):
= {
kw if o.default == Parameter.empty else o.default)
n: (o.annotation, ... for n, o in inspect.signature(f).parameters.items()
}= create_model(f"Input for `{f.__name__}`", **kw).model_json_schema()
s return dict(name=f.__name__, description=f.__doc__, parameters=s)
schema(sums)
{'name': 'sums',
'description': 'Adds a + b',
'parameters': {'properties': {'a': {'title': 'A', 'type': 'integer'},
'b': {'default': 1, 'title': 'B', 'type': 'integer'}},
'required': ['a'],
'title': 'Input for `sums`',
'type': 'object'}}
= askgpt(
c "Use the `sum` function to solve this: What is 6+3?",
="You must use the `sum` function instead of adding yourself.",
system=[schema(sums)],
functions )
c
ChatCompletion(id='chatcmpl-8fmz35qBq4bV1VOi2FWuzLtJPeSDm', choices=[Choice(finish_reason='function_call', index=0, logprobs=None, message=ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{\n "a": 6,\n "b": 3\n}', name='sums'), tool_calls=None))], created=1704970433, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=22, prompt_tokens=83, total_tokens=105))
= c.choices[0].message m
m
ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{\n "a": 6,\n "b": 3\n}', name='sums'), tool_calls=None)
m.function_call.arguments
'{\n "a": 6,\n "b": 3\n}'
= {"sums", "python"}
funcs_ok
def call_func(c):
= c.choices[0].message.function_call
fc if fc.name not in funcs_ok:
return print(f"Not allowed: {fc.name}")
= globals()[fc.name]
f return f(**json.loads(fc.arguments))
call_func(c)
9
run("""
a=1
b=2
a+b
"""
)
3
= askgpt(
c "What is 12 factorial?",
="Use python for any required computations.",
system=[schema(python)],
functions )
call_func(c)
479001600