Skip to content

Commit 1e93d08

Browse files
cebtenzzrehodlen
authored andcommitted
examples : make pydantic scripts pass mypy and support py3.8 (ggml-org#5099)
1 parent 8e3970e commit 1e93d08

File tree

2 files changed

+88
-121
lines changed

2 files changed

+88
-121
lines changed

examples/pydantic-models-to-grammar-examples.py

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Function calling example using pydantic models.
22
import datetime
3+
import importlib
34
import json
45
from enum import Enum
5-
from typing import Union, Optional
6+
from typing import Optional, Union
67

78
import requests
89
from pydantic import BaseModel, Field
9-
10-
import importlib
11-
from pydantic_models_to_grammar import generate_gbnf_grammar_and_documentation, convert_dictionary_to_pydantic_model, add_run_method_to_dynamic_model, create_dynamic_model_from_function
10+
from pydantic_models_to_grammar import (add_run_method_to_dynamic_model, convert_dictionary_to_pydantic_model,
11+
create_dynamic_model_from_function, generate_gbnf_grammar_and_documentation)
1212

1313

1414
# Function to get completion on the llama.cpp server with grammar.
@@ -35,15 +35,15 @@ def run(self):
3535
print(self.message)
3636

3737

38-
# Enum for the calculator function.
38+
# Enum for the calculator tool.
3939
class MathOperation(Enum):
4040
ADD = "add"
4141
SUBTRACT = "subtract"
4242
MULTIPLY = "multiply"
4343
DIVIDE = "divide"
4444

4545

46-
# Very simple calculator tool for the agent.
46+
# Simple pydantic calculator tool for the agent that can add, subtract, multiply, and divide. Docstring and description of fields will be used in system prompt.
4747
class Calculator(BaseModel):
4848
"""
4949
Perform a math operation on two numbers.
@@ -148,37 +148,6 @@ def get_current_datetime(output_format: Optional[str] = None):
148148
return datetime.datetime.now().strftime(output_format)
149149

150150

151-
# Enum for the calculator tool.
152-
class MathOperation(Enum):
153-
ADD = "add"
154-
SUBTRACT = "subtract"
155-
MULTIPLY = "multiply"
156-
DIVIDE = "divide"
157-
158-
159-
160-
# Simple pydantic calculator tool for the agent that can add, subtract, multiply, and divide. Docstring and description of fields will be used in system prompt.
161-
class Calculator(BaseModel):
162-
"""
163-
Perform a math operation on two numbers.
164-
"""
165-
number_one: Union[int, float] = Field(..., description="First number.")
166-
operation: MathOperation = Field(..., description="Math operation to perform.")
167-
number_two: Union[int, float] = Field(..., description="Second number.")
168-
169-
def run(self):
170-
if self.operation == MathOperation.ADD:
171-
return self.number_one + self.number_two
172-
elif self.operation == MathOperation.SUBTRACT:
173-
return self.number_one - self.number_two
174-
elif self.operation == MathOperation.MULTIPLY:
175-
return self.number_one * self.number_two
176-
elif self.operation == MathOperation.DIVIDE:
177-
return self.number_one / self.number_two
178-
else:
179-
raise ValueError("Unknown operation.")
180-
181-
182151
# Example function to get the weather
183152
def get_current_weather(location, unit):
184153
"""Get the current weather in a given location"""

0 commit comments

Comments
 (0)