Update README.md
Browse files
README.md
CHANGED
|
@@ -54,7 +54,57 @@ print(response)
|
|
| 54 |
### Basic
|
| 55 |
|
| 56 |
```python
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
```
|
| 59 |
|
| 60 |
### Advanced
|
|
@@ -65,15 +115,6 @@ from enum import Enum
|
|
| 65 |
from pydantic import BaseModel, Field # pip install pydantic
|
| 66 |
from instructor.function_calls import openai_schema # pip install instructor
|
| 67 |
|
| 68 |
-
def get_prompt(tool: str, user_input: str) -> str:
|
| 69 |
-
system = "You are a helpful assistant with access to the following tools. Use them if required - \n```json\n{}\n```"
|
| 70 |
-
messages = [
|
| 71 |
-
{"role": "system", "content": system.format(tool)},
|
| 72 |
-
{"role": "user", "content": 'Extract the information from the following - \n{}'.format(user_input)}
|
| 73 |
-
]
|
| 74 |
-
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 75 |
-
return prompt
|
| 76 |
-
|
| 77 |
# Define functions using pydantic classes
|
| 78 |
class PaperCategory(str, Enum):
|
| 79 |
TYPE_1_DIABETES = 'Type 1 Diabetes'
|
|
@@ -90,7 +131,13 @@ prompt = get_prompt(json.dumps(tool), input_text)
|
|
| 90 |
output = inference(prompt)
|
| 91 |
print(output)
|
| 92 |
# ```json
|
| 93 |
-
# {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
# ```
|
| 95 |
# Extract JSON string using regex
|
| 96 |
output = re.search(r'```json\s*(\{.*?\})\s*```', output).group(1)
|
|
|
|
| 54 |
### Basic
|
| 55 |
|
| 56 |
```python
|
| 57 |
+
import json
|
| 58 |
|
| 59 |
+
def get_prompt(tool: str, user_input: str) -> str:
|
| 60 |
+
system = "You are a helpful assistant with access to the following tools. Use them if required - \n```json\n{}\n```"
|
| 61 |
+
messages = [
|
| 62 |
+
{"role": "system", "content": system.format(tool)},
|
| 63 |
+
{"role": "user", "content": 'Extract the information from the following - \n{}'.format(user_input)}
|
| 64 |
+
]
|
| 65 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 66 |
+
return prompt
|
| 67 |
+
|
| 68 |
+
tool = {
|
| 69 |
+
"type": "function",
|
| 70 |
+
"function": {
|
| 71 |
+
"name": "get_company_info",
|
| 72 |
+
"description": "Correctly extracted company information with all the required parameters with correct types",
|
| 73 |
+
"parameters": {
|
| 74 |
+
"properties": {
|
| 75 |
+
"name": {"title": "Name", "type": "string"},
|
| 76 |
+
"investors": {
|
| 77 |
+
"items": {"type": "string"},
|
| 78 |
+
"title": "Investors",
|
| 79 |
+
"type": "array"
|
| 80 |
+
},
|
| 81 |
+
"valuation": {"title": "Valuation", "type": "string"},
|
| 82 |
+
"source": {"title": "Source", "type": "string"}
|
| 83 |
+
},
|
| 84 |
+
"required": ["investors", "name", "source", "valuation"],
|
| 85 |
+
"type": "object"
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
input_text = "Founded in 2021, Pluto raised $4 million across multiple seed funding rounds, valuing the company at $12 million (pre-money), according to PitchBook. The startup was backed by investors including Switch Ventures, Caffeinated Capital and Maxime Seguineau."
|
| 90 |
+
prompt = get_prompt(json.dumps(tool), input_text)
|
| 91 |
+
response = inference(prompt)
|
| 92 |
+
print(response)
|
| 93 |
+
# ```json
|
| 94 |
+
# {
|
| 95 |
+
# "name": "get_company_info",
|
| 96 |
+
# "arguments": {
|
| 97 |
+
# "name": "Pluto",
|
| 98 |
+
# "investors": [
|
| 99 |
+
# "Switch Ventures",
|
| 100 |
+
# "Caffeinated Capital",
|
| 101 |
+
# "Maxime Seguineau"
|
| 102 |
+
# ],
|
| 103 |
+
# "valuation": "pre-money $12M",
|
| 104 |
+
# "source": "PitchBook"
|
| 105 |
+
# }
|
| 106 |
+
# }
|
| 107 |
+
# ```
|
| 108 |
```
|
| 109 |
|
| 110 |
### Advanced
|
|
|
|
| 115 |
from pydantic import BaseModel, Field # pip install pydantic
|
| 116 |
from instructor.function_calls import openai_schema # pip install instructor
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# Define functions using pydantic classes
|
| 119 |
class PaperCategory(str, Enum):
|
| 120 |
TYPE_1_DIABETES = 'Type 1 Diabetes'
|
|
|
|
| 131 |
output = inference(prompt)
|
| 132 |
print(output)
|
| 133 |
# ```json
|
| 134 |
+
# {
|
| 135 |
+
# "name": "Classification",
|
| 136 |
+
# "arguments": {
|
| 137 |
+
# "label": "Type 1 Diabetes",
|
| 138 |
+
# "reason": "The study investigated the effect of vitamin D status and treatment with 1,25(OH)(2)D(3) on diabetes onset in non-obese diabetic (NOD) mice. It also concluded that vitamin D deficiency leads to an increase in diabetes incidence and that the addition of 1,25(OH)(2)D(3) can prevent diabetes onset in NOD mice."
|
| 139 |
+
# }
|
| 140 |
+
# }
|
| 141 |
# ```
|
| 142 |
# Extract JSON string using regex
|
| 143 |
output = re.search(r'```json\s*(\{.*?\})\s*```', output).group(1)
|