You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

274 lines
8.9 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "9aed4e5e-bb3e-422c-9178-1eaada1fbbe7",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26da8d29-2a54-468e-93ff-2c732a89872d",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv()\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
" \n",
"MODEL = \"gpt-4o-mini\"\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1b5bb0a-6862-49ee-83c9-3e1a7ba212b1",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant for questions regarding geography. \"\n",
"system_message += \"Give short, friendly answers, no more than 2 sentences. You are currently still under construction, but\\\n",
"you already have a lot of knowledge at your disposal. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bf33946a-4737-4882-9fb9-2707867a3fae",
"metadata": {},
"outputs": [],
"source": [
"#Here are the knowledge lists:\n",
"\n",
"country_capitals = {\"france\": \"Paris\", \"belgium\": \"Brussels\", \"mali\": \"Bamako\"}\n",
"country_populations = {\"france\": 68_170_000, \"belgium\": 11_820_000, \"mali\": 23_290_000}\n",
"capital_populations = {\"france\": 2_103_000, \"belgium\": 188_737, \"mali\": 4_228_000}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a60dcf1-9ef5-4e67-9aad-0a471f6e794a",
"metadata": {},
"outputs": [],
"source": [
"def get_country_capital(country):\n",
" print(f\"Tool get_country_capital called for {country}\")\n",
" data_country = country.lower()\n",
" return country_capitals.get(data_country, 'Unknown') #This is a default"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56585ff9-11c0-4057-8f5c-b87060103c74",
"metadata": {},
"outputs": [],
"source": [
"get_country_capital(\"Botswana\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f18c051a-1bd0-4951-9fdb-a96199361320",
"metadata": {},
"outputs": [],
"source": [
"def get_country_population(country):\n",
" print(f\"Tool get_country_population called for {country}\")\n",
" data_country = country.lower()\n",
" return str(country_populations.get(data_country, 'Unknown'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "30081159-c29a-49e9-8727-d27e609a6f39",
"metadata": {},
"outputs": [],
"source": [
"def get_capital_population(country):\n",
" print(f\"Tool get_capital_population called for {country}\")\n",
" data_country = country.lower()\n",
" return str(capital_populations.get(data_country, 'Unknown'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b54f7ee6-50ee-4153-907a-aa2ab3e52c5b",
"metadata": {},
"outputs": [],
"source": [
"country_capital_function = {\n",
" \"name\": \"get_country_capital\",\n",
" \"description\": \"Get the capital city of a country. Call this whenever you need to know what a country's capital is, for example when a user asks 'What's the capital of this country?'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"country\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The country the user wants to know the capital of\",\n",
" },\n",
" },\n",
" \"required\": [\"country\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b554d24-3d64-4bd2-bb3e-015efcaaa69b",
"metadata": {},
"outputs": [],
"source": [
"country_population_function = {\n",
" \"name\": \"get_country_population\",\n",
" \"description\": \"Get the population of a country. Call this whenever you need to know how many people live in a country, for example \\\n",
"when a user asks 'What's the population of this country?'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"country\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The country the user wants to know the population of\",\n",
" },\n",
" },\n",
" \"required\": [\"country\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0cc418a0-443a-4445-95b1-f59a98ff294e",
"metadata": {},
"outputs": [],
"source": [
"capital_population_function = {\n",
" \"name\": \"get_capital_population\",\n",
" \"description\": \"Get the population of a country's capital. Call this whenever you need to know how many people live in the capital of a country\\\n",
", for example when a user asks 'How many people live in the capital of this country?'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"country\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The country whose capital the user wants to know the population of\",\n",
" },\n",
" },\n",
" \"required\": [\"country\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8fad366-a911-42cb-8bc3-687241bd9557",
"metadata": {},
"outputs": [],
"source": [
"tools = [{\"type\": \"function\", \"function\": country_capital_function}, {\"type\": \"function\", \"function\": country_population_function}, {\"type\": \"function\", \"function\": capital_population_function}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8660f43e-1c80-4cd1-b1ad-90753f9263c2",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
"\n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" responses = handle_tool_call(message)\n",
" messages.append(message)\n",
" for response in responses:\n",
" messages.append(response)\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
"\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3fa504e-50f8-4d38-a856-be06b00dc53d",
"metadata": {},
"outputs": [],
"source": [
"def handle_tool_call(message):\n",
" responses = []\n",
" for tool_call in message.tool_calls:\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" indata = arguments.get(\"country\") #in case of different functions with different numbers of arguments, this part will have to be 'abstracted'\n",
" function_name = tool_call.function.name\n",
" if function_name == 'get_country_capital':\n",
" outdata = get_country_capital(indata)\n",
" elif function_name == 'get_country_population':\n",
" outdata = get_country_population(indata)\n",
" else:\n",
" outdata = get_capital_population(indata)\n",
" \n",
" responses.append({\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"country\": indata,\"outdata\": outdata}),\n",
" \"tool_call_id\": tool_call.id\n",
" })\n",
" return responses"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1893cb4b-bd61-4617-85db-8698d98516b1",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}