Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools and functions #110

Merged
merged 10 commits into from
Feb 22, 2024
34 changes: 34 additions & 0 deletions examples/logging/magentic_async_function_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import openai
from magentic import AsyncStreamedStr, FunctionCall, prompt

from log10.load import log10


log10(openai)


def add(x: int, y: int) -> int:
"""Add together two numbers."""
return x + y


@prompt("What is 1+1? Use tools", functions=[add])
async def agent() -> AsyncStreamedStr:
...


# Define an async main function
async def main():
response = await agent()
if isinstance(response, FunctionCall):
print(response)
else:
async for chunk in response:
print(chunk, end="", flush=True)


# Running the main function using asyncio
if __name__ == "__main__":
import asyncio

asyncio.run(main())
31 changes: 31 additions & 0 deletions examples/logging/magentic_function_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# taken from Magentic README
# https://github.com/jackmpcollins/magentic/blob/2493419f2db3a3be58fb308d7df51a51bf1989c1/README.md#usage

from typing import Literal

import openai
from magentic import FunctionCall, prompt

from log10.load import log10


log10(openai)


def activate_oven(temperature: int, mode: Literal["broil", "bake", "roast"]) -> str:
"""Turn the oven on with the provided settings."""
return f"Preheating to {temperature} F with mode {mode}"


@prompt(
"Prepare the oven so I can make {food}",
functions=[activate_oven],
)
def configure_oven(food: str) -> FunctionCall[str]:
...


output = configure_oven("cookies!")
# FunctionCall(<function activate_oven at 0x1105a6200>, temperature=350, mode='bake')
print(output())
# 'Preheating to 350 F with mode bake'
91 changes: 91 additions & 0 deletions examples/logging/openai_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import json

from log10.load import OpenAI


client = OpenAI()


# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""
if "tokyo" in location.lower():
return json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})
elif "san francisco" in location.lower():
return json.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})
elif "paris" in location.lower():
return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
else:
return json.dumps({"location": location, "temperature": "unknown"})


def run_conversation():
# Step 1: send the conversation and available functions to the model
messages = [
{
"role": "user",
"content": "What's the weather like in San Francisco, Tokyo, and Paris?",
}
]
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=messages,
tools=tools,
tool_choice="auto", # auto is default, but we'll be explicit
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
# Step 2: check if the model wanted to call a function
if tool_calls:
# Step 3: call the function
# Note: the JSON response may not always be valid; be sure to handle errors
available_functions = {
"get_current_weather": get_current_weather,
} # only one function in this example, but you can have multiple
messages.append(response_message) # extend conversation with assistant's reply
# Step 4: send the info for each function call and function response to the model
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
function_response = function_to_call(
location=function_args.get("location"),
unit=function_args.get("unit"),
)
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
) # extend conversation with function response
second_response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=messages,
) # get a new response from the model where it can see the function response
return second_response


print(run_conversation())
38 changes: 34 additions & 4 deletions log10/_httpx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,48 @@ async def aiter_bytes(self, *args, **kwargs):
for frame in current_stack_frame
]
full_content = ""
function_name = ""
full_argument = ""
responses = full_response.split("\n\n")
for r in responses:
if "data: [DONE]" in r:
break

r_json = json.loads(r[6:])
content = r_json["choices"][0]["delta"].get("content", "")
if content:
full_content += content

delta = r_json["choices"][0]["delta"]

# Delta may have content
if "content" in delta:
content = delta["content"]
if content:
full_content += content

# May be a function call, and have to reconstruct the arguments
if "function_call" in delta:
# May be function name
if "name" in delta["function_call"]:
function_name = delta["function_call"]["name"]
# May be function arguments
if "arguments" in delta["function_call"]:
full_argument += delta["function_call"]["arguments"]

response_json = r_json.copy()
response_json["object"] = "completion"
response_json["choices"][0]["message"] = {"role": "assistant", "content": full_content}

# If finish_reason is function_call - don't log the response
if not (
"choices" in response_json
and response_json["choices"]
and response_json["choices"][0]["finish_reason"] == "function_call"
):
response_json["choices"][0]["message"] = {"role": "assistant", "content": full_content}
else:
response_json["choices"][0]["function_call"] = {
"name": function_name,
"arguments": full_argument,
}

log_row = {
"response": json.dumps(response_json),
"status": "finished",
Expand Down
Loading
Loading