참고소스 수정본
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
{"messages": [{"role": "system", "content": "Predict the results of this function:\n\ndef fn(a: int, b: int, c: str) -> __main__.Multiply\n\"\"\"\n_summary_\n\nArgs:\n a (int): _description_\n b (int): _description_\n c (str): _description_\n\nReturns:\n Response: _description_\n\"\"\""}, {"role": "user", "content": "Return `fn(540, b=677, c=\"hello\")`"}, {"role": "assistant", "function_call": {"name": "Multiply", "arguments": "{\n \"a\": 540,\n \"b\": 677,\n \"result\": 1217\n}"}}], "functions": [{"name": "Multiply", "description": "Correctly extracted `Multiply` with all the required parameters with correct types", "parameters": {"properties": {"a": {"type": "integer"}, "b": {"type": "integer"}, "result": {"description": "The result of the multiplication", "type": "integer"}}, "required": ["a", "b", "result"], "type": "object"}}]}
|
||||
{"messages": [{"role": "system", "content": "Predict the results of this function:\n\ndef fn(a: int, b: int, c: str) -> __main__.Multiply\n\"\"\"\n_summary_\n\nArgs:\n a (int): _description_\n b (int): _description_\n c (str): _description_\n\nReturns:\n Response: _description_\n\"\"\""}, {"role": "user", "content": "Return `fn(798, b=534, c=\"hello\")`"}, {"role": "assistant", "function_call": {"name": "Multiply", "arguments": "{\n \"a\": 798,\n \"b\": 534,\n \"result\": 1332\n}"}}], "functions": [{"name": "Multiply", "description": "Correctly extracted `Multiply` with all the required parameters with correct types", "parameters": {"properties": {"a": {"type": "integer"}, "b": {"type": "integer"}, "result": {"description": "The result of the multiplication", "type": "integer"}}, "required": ["a", "b", "result"], "type": "object"}}]}
|
||||
{"messages": [{"role": "system", "content": "Predict the results of this function:\n\ndef fn(a: int, b: int, c: str) -> __main__.Multiply\n\"\"\"\n_summary_\n\nArgs:\n a (int): _description_\n b (int): _description_\n c (str): _description_\n\nReturns:\n Response: _description_\n\"\"\""}, {"role": "user", "content": "Return `fn(608, b=669, c=\"hello\")`"}, {"role": "assistant", "function_call": {"name": "Multiply", "arguments": "{\n \"a\": 608,\n \"b\": 669,\n \"result\": 1277\n}"}}], "functions": [{"name": "Multiply", "description": "Correctly extracted `Multiply` with all the required parameters with correct types", "parameters": {"properties": {"a": {"type": "integer"}, "b": {"type": "integer"}, "result": {"description": "The result of the multiplication", "type": "integer"}}, "required": ["a", "b", "result"], "type": "object"}}]}
|
||||
{"messages": [{"role": "system", "content": "Predict the results of this function:\n\ndef fn(a: int, b: int, c: str) -> __main__.Multiply\n\"\"\"\n_summary_\n\nArgs:\n a (int): _description_\n b (int): _description_\n c (str): _description_\n\nReturns:\n Response: _description_\n\"\"\""}, {"role": "user", "content": "Return `fn(982, b=768, c=\"hello\")`"}, {"role": "assistant", "function_call": {"name": "Multiply", "arguments": "{\n \"a\": 982,\n \"b\": 768,\n \"result\": 1750\n}"}}], "functions": [{"name": "Multiply", "description": "Correctly extracted `Multiply` with all the required parameters with correct types", "parameters": {"properties": {"a": {"type": "integer"}, "b": {"type": "integer"}, "result": {"description": "The result of the multiplication", "type": "integer"}}, "required": ["a", "b", "result"], "type": "object"}}]}
|
||||
47
참고/instructor-main/examples/distilations/readme.md
Normal file
47
참고/instructor-main/examples/distilations/readme.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# What to Expect
|
||||
This script demonstrates how to use the `Instructor` library for fine-tuning a Python function that performs three-digit multiplication. It uses Pydantic for type validation and logging features to generate a fine-tuning dataset.
|
||||
|
||||
## How to Run
|
||||
|
||||
### Prerequisites
|
||||
- Python 3.9
|
||||
- `Instructor` library
|
||||
|
||||
### Steps
|
||||
1. **Install Dependencies**
|
||||
If you haven't already installed the required libraries, you can do so using pip:
|
||||
```
|
||||
pip install instructor pydantic
|
||||
```
|
||||
|
||||
2. **Set Up Logging**
|
||||
The script uses Python's built-in `logging` module to log the fine-tuning process. Ensure you have write permissions in the directory where the log file `math_finetunes.jsonl` will be saved.
|
||||
|
||||
3. **Run the Script**
|
||||
Navigate to the directory containing `script.py` and run it:
|
||||
```
|
||||
python three_digit_mul.py
|
||||
```
|
||||
|
||||
This will execute the script, running the function ten times with random three-digit numbers for multiplication. The function outputs and logs are saved in `math_finetunes.jsonl`.
|
||||
|
||||
4. **Fine-Tuning**
|
||||
Once you have the log file, you can run a fine-tuning job using the following `Instructor` CLI command:
|
||||
```
|
||||
instructor jobs create-from-file math_finetunes.jsonl
|
||||
```
|
||||
Wait for the fine-tuning job to complete.
|
||||
|
||||
If you have validation date you can run:
|
||||
|
||||
```
|
||||
instructor jobs create-from-file math_finetunes.jsonl --n-epochs 4 --validation-file math_finetunes_val.jsonl
|
||||
```
|
||||
|
||||
### Output
|
||||
|
||||
That's it! You've successfully run the script and can now proceed to fine-tune your model.
|
||||
|
||||
### Dispatch
|
||||
|
||||
Once you have the model you can replace the model in `three_digit_mul_dispatch.py` with the model you just fine-tuned and run the script again. This time, the script will use the fine-tuned model to predict the output of the function.
|
||||
72
참고/instructor-main/examples/distilations/three_digit_mul.py
Normal file
72
참고/instructor-main/examples/distilations/three_digit_mul.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import logging
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from instructor import Instructions
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# Usage
|
||||
instructions = Instructions(
|
||||
name="three_digit_multiply",
|
||||
finetune_format="messages",
|
||||
log_handlers=[
|
||||
logging.FileHandler("math_finetunes.jsonl"),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
class Multiply(BaseModel):
|
||||
a: int
|
||||
b: int
|
||||
result: int = Field(..., description="The result of the multiplication")
|
||||
|
||||
|
||||
@instructions.distil
|
||||
def fn(a: int, b: int) -> Multiply:
|
||||
"""Return the result of multiplying a and b together"""
|
||||
resp = a * b
|
||||
return Multiply(a=a, b=b, result=resp)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import random
|
||||
|
||||
log_lines = {
|
||||
"messages": [
|
||||
{
|
||||
"role": "system",
|
||||
"content": 'Predict the results of this function:\n\ndef fn(a: int, b: int) -> __main__.Multiply\n"""\nReturn the result of multiplying a and b together\n"""',
|
||||
},
|
||||
{"role": "user", "content": "Return `fn(169, b=166)`"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"function_call": {
|
||||
"name": "Multiply",
|
||||
"arguments": '{\n "a": 169,\n "b": 166,\n "result": 28054\n}',
|
||||
},
|
||||
},
|
||||
],
|
||||
"functions": [
|
||||
{
|
||||
"name": "Multiply",
|
||||
"description": "Correctly extracted `Multiply` with all the required parameters with correct types",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"a": {"title": "A", "type": "integer"},
|
||||
"b": {"title": "B", "type": "integer"},
|
||||
"result": {
|
||||
"description": "The result of the multiplication",
|
||||
"title": "Result",
|
||||
"type": "integer",
|
||||
},
|
||||
},
|
||||
"required": ["a", "b", "result"],
|
||||
"type": "object",
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
for _ in range(10):
|
||||
a = random.randint(100, 999)
|
||||
b = random.randint(100, 999)
|
||||
print("returning", fn(a, b=b))
|
||||
@@ -0,0 +1,51 @@
|
||||
import logging
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from instructor import Instructions
|
||||
import instructor
|
||||
from openai import OpenAI
|
||||
|
||||
client = instructor.from_openai(OpenAI())
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# Usage
|
||||
instructions = Instructions(
|
||||
name="three_digit_multiply",
|
||||
finetune_format="messages",
|
||||
include_code_body=True,
|
||||
log_handlers=[
|
||||
logging.FileHandler("math_finetunes.jsonl"),
|
||||
],
|
||||
openai_client=client,
|
||||
)
|
||||
|
||||
|
||||
class Multiply(BaseModel):
|
||||
a: int
|
||||
b: int
|
||||
result: int = Field(..., description="The result of the multiplication")
|
||||
|
||||
|
||||
@instructions.distil(mode="dispatch", model="ft:gpt-3.5-turbo-0125:personal::9i1JeuxJ")
|
||||
def fn(a: int, b: int) -> Multiply:
|
||||
"""Return the result of the multiplication as an integer"""
|
||||
resp = a * b
|
||||
return Multiply(a=a, b=b, result=resp)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import random
|
||||
|
||||
for _ in range(5):
|
||||
a = random.randint(100, 999)
|
||||
b = random.randint(100, 999)
|
||||
result = fn(a, b)
|
||||
print(f"{a} * {b} = {result.result}, expected {a * b}")
|
||||
"""
|
||||
972 * 508 = 493056, expected 493776
|
||||
145 * 369 = 53505, expected 53505
|
||||
940 * 440 = 413600, expected 413600
|
||||
114 * 213 = 24282, expected 24282
|
||||
259 * 650 = 168350, expected 168350
|
||||
"""
|
||||
Reference in New Issue
Block a user