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

Feat/function calling #1472

Merged
merged 18 commits into from
Oct 15, 2024
Merged

Feat/function calling #1472

merged 18 commits into from
Oct 15, 2024

Conversation

nguyenhoangthuan99
Copy link
Contributor

@nguyenhoangthuan99 nguyenhoangthuan99 commented Oct 14, 2024

Describe Your Changes

Fix #295

Example usage

import json
from datetime import datetime
from openai import OpenAI
ENDPOINT = "http://localhost:39281/v1" 
MODEL = "llama3.1:8b-gguf-q4-km"



tools = [
    {
        "type": "function",
        "function": {
            "name": "get_delivery_date",
            "description": "Get the delivery date for a customer's order. Call this whenever you need to know the delivery date, for example when a customer asks 'Where is my package'",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {
                        "type": "string",
                        "description": "The customer's order ID.",
                    },
                },
                "required": ["order_id"],
                "additionalProperties": False,
            },
        }
    }
]

messages = [
    {"role": "user", "content": "Hi, can you tell me the delivery date for my order order_12345?"}
]

order_id = "order_12345"
delivery_date = datetime.now()

# Simulate the tool call response
response = {
    "choices": [
        {
            "message": {
                "role": "assistant",
                "tool_calls": [
                    {
                        "id": "call_62136354",
                        "type": "function",
                        "function": {
                            "arguments": "{'order_id': 'order_12345'}",
                            "name": "get_delivery_date"
                        }
                    }
                ]
            }
        }
    ]
}

# Create a message containing the result of the function call
function_call_result_message = {
    "role": "tool",
    "content": json.dumps({
        "order_id": order_id,
        "delivery_date": delivery_date.strftime('%Y-%m-%d %H:%M:%S')
    }),
    "tool_call_id": response['choices'][0]['message']['tool_calls'][0]['id']
}

# Prepare the chat completion call payload
completion_payload = {
    "messages": [
        {"role": "system", "content": "You are a helpful customer support assistant. Use the supplied tools to assist the user."},
        {"role": "user", "content": "Hi, can you tell me the delivery date for my order?"},
        {"role": "assistant", "content": "Hi there! I can help with that. Can you please provide your order ID?"},
        {"role": "user", "content": "i think it is order_12345"},
    ]
}
client = OpenAI(
    # This is the default and can be omitted
    base_url=ENDPOINT,
    api_key="not-needed"
)

response = client.chat.completions.create(
    model=MODEL,
    messages=completion_payload["messages"],
    tools=tools,
    stream=False
)

print(json.dumps(response.dict(), indent=4))

After finished, the output response should be

{
    "id": "QN5T5hxLeim4ntaNGEtX",
    "choices": [
        {
            "finish_reason": "tool_calls",
            "index": 0,
            "logprobs": null,
            "message": {
                "content": "",
                "refusal": null,
                "role": "assistant",
                "function_call": null,
                "tool_calls": [
                    {
                        "id": null,
                        "function": {
                            "arguments": "{\"order_id\": \"order_12345\"}",
                            "name": "get_delivery_date"
                        },
                        "type": "function"
                    }
                ]
            }
        }
    ],
    "created": 1728987758,
    "model": "_",
    "object": "chat.completion",
    "service_tier": null,
    "system_fingerprint": "_",
    "usage": {
        "completion_tokens": 18,
        "prompt_tokens": 442,
        "total_tokens": 460,
        "completion_tokens_details": null,
        "prompt_tokens_details": null
    }
}

@nguyenhoangthuan99 nguyenhoangthuan99 marked this pull request as ready for review October 15, 2024 09:51
@nguyenhoangthuan99 nguyenhoangthuan99 merged commit 21d77bc into dev Oct 15, 2024
4 checks passed
@nguyenhoangthuan99 nguyenhoangthuan99 deleted the feat/function-calling branch October 15, 2024 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Cortex supports Function Calling
2 participants