Skip to content

Commit

Permalink
include a basic exercise that uses the OpenAI API with the test model
Browse files Browse the repository at this point in the history
  • Loading branch information
rgbkrk committed Nov 7, 2023
1 parent 5eca2e9 commit 587cacd
Showing 1 changed file with 169 additions and 0 deletions.
169 changes: 169 additions & 0 deletions tests/ModelModelModels.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from chatlab import Chat"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from typing import Optional\n",
"from pydantic import BaseModel, Field\n",
"\n",
"# Define a function to use in testing\n",
"def simple_func(x: int, y: str, z: bool = False):\n",
" \"\"\"A simple test function\"\"\"\n",
" return f\"{x}, {y}, {z}\"\n",
"\n",
"\n",
"class SimpleModel(BaseModel):\n",
" x: int\n",
" y: str\n",
" z: bool = Field(default=False, description=\"A simple boolean field\")\n",
"\n",
"\n",
"class SimpleClass:\n",
" def simple_method(self, x: int, y: str, z: bool = False):\n",
" \"\"\"A simple test method\"\"\"\n",
" return f\"{x}, {y}, {z}\"\n",
"\n",
"\n",
"def simple_func_with_model_arg(\n",
" x: int,\n",
" y: str,\n",
" z: bool = False,\n",
" model: Optional[SimpleModel] = None,\n",
") -> str:\n",
" \"\"\"A simple test function with a model argument\"\"\"\n",
" return f\"{x}, {y}, {z}, {model}\"\n",
"\n",
"\n",
"class NestedModel(BaseModel):\n",
" foo: int\n",
" bar: str\n",
" baz: bool = True\n",
" simple_model: SimpleModel\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def simple_func_with_model_args(\n",
" x: int,\n",
" y: str,\n",
" z: bool = False,\n",
" model: Optional[SimpleModel] = None,\n",
" nested_model: Optional[NestedModel] = None,\n",
") -> str:\n",
" \"\"\"A simple test function with model arguments\"\"\"\n",
" return f\"{x}, {y}, {z}, {model}, {nested_model}\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><style>.chatlab-chat-details summary &gt; * { display: inline; color: #27374D; }</style><details style=\"background: #DDE6ED; border-radius: 5px; padding: .5rem 1rem\" className=\"chatlab-chat-details\"><summary style=\"color: #27374D; cursor: pointer\"><span style=\"color: #9DB2BF; padding-left: 5px; padding-right: 5px\">𝑓</span><span style=\"color: #27374D; padding-left: 5px; padding-right: 5px\">Ran</span><span style=\"font-family: monospace; unicode-bidi: embed; white-space: pre\">simple_func_with_model_args</span><span style=\"font-family: monospace; unicode-bidi: embed; white-space: pre\"></span></summary><div style=\"margin-left: 10px; margin-top: 10px\"><div><div style=\"color: #27374D; font-weight: 500; margin-bottom: 5px\">Input:</div><div style=\"background: #F7F9FA; color: #27374D; font-family: monospace; margin-bottom: 10px; overflow-x: auto; padding: 10px; unicode-bidi: embed; white-space: pre\">{\n",
" &quot;x&quot;: 10,\n",
" &quot;y&quot;: &quot;example&quot;,\n",
" &quot;z&quot;: true,\n",
" &quot;model&quot;: {\n",
" &quot;x&quot;: 5,\n",
" &quot;y&quot;: &quot;model&quot;,\n",
" &quot;z&quot;: false\n",
" },\n",
" &quot;nested_model&quot;: {\n",
" &quot;foo&quot;: 1,\n",
" &quot;bar&quot;: &quot;nested&quot;,\n",
" &quot;baz&quot;: true,\n",
" &quot;simple_model&quot;: {\n",
" &quot;x&quot;: 2,\n",
" &quot;y&quot;: &quot;nested_model&quot;,\n",
" &quot;z&quot;: true\n",
" }\n",
" }\n",
"}</div></div><div><div style=\"color: #27374D; font-weight: 500; margin-bottom: 5px\">Output:</div><div style=\"background: #F7F9FA; color: #27374D; font-family: monospace; margin-bottom: 10px; overflow-x: auto; padding: 10px; unicode-bidi: embed; white-space: pre\">10, example, True, {&#x27;x&#x27;: 5, &#x27;y&#x27;: &#x27;model&#x27;, &#x27;z&#x27;: False}, {&#x27;foo&#x27;: 1, &#x27;bar&#x27;: &#x27;nested&#x27;, &#x27;baz&#x27;: True, &#x27;simple_model&#x27;: {&#x27;x&#x27;: 2, &#x27;y&#x27;: &#x27;nested_model&#x27;, &#x27;z&#x27;: True}}</div></div></div></details></div>"
],
"text/plain": [
"<chatlab.display.ChatFunctionCall at 0x10ff36250>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"The simple_func_with_model_args function was called with the following data:\n",
"\n",
"- x: 10\n",
"- y: \"example\"\n",
"- z: true\n",
"- model: {x: 5, y: \"model\", z: false}\n",
"- nested_model: {foo: 1, bar: \"nested\", baz: true, simple_model: {x: 2, y: \"nested_model\", z: true}}"
],
"text/plain": [
"The simple_func_with_model_args function was called with the following data:\n",
"\n",
"- x: 10\n",
"- y: \"example\"\n",
"- z: true\n",
"- model: {x: 5, y: \"model\", z: false}\n",
"- nested_model: {foo: 1, bar: \"nested\", baz: true, simple_model: {x: 2, y: \"nested_model\", z: true}}"
]
},
"metadata": {
"text/markdown": {
"chatlab": {
"default": true
}
}
},
"output_type": "display_data"
}
],
"source": [
"chat = Chat()\n",
"chat.register(simple_func_with_model_args)\n",
"\n",
"await chat(\"Call my simple func with some example data\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "chatlab-3kMKfU-i-py3.11",
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 587cacd

Please sign in to comment.