File size: 23,699 Bytes
98ad598 7a98779 61f939d 7a98779 61f939d 8b3a604 61f939d 7a98779 252d783 e7a3afa e81aa82 e7a3afa 427dcf2 e7a3afa 427dcf2 e7a3afa e81aa82 e7a3afa 427dcf2 e7a3afa 427dcf2 e7a3afa 427dcf2 e7a3afa 427dcf2 e7a3afa 427dcf2 e7a3afa e81aa82 e7a3afa 427dcf2 e7a3afa 427dcf2 e7a3afa 427dcf2 e7a3afa 65bad10 462da2b 65bad10 8b3a604 65bad10 462da2b 65bad10 daa1f12 65bad10 40c04ce 65bad10 8b3a604 7a98779 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | import os
import gradio as gr
from anthropic import Anthropic
client = Anthropic()
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
MODEL_NAME = "claude-3-opus-20240229"
# Tools for Claude
tools = [
{
"name": "interactions_schema",
"description": "Record from an interview transcript of an interaction between a Technical Assistance Provider (TAP) and a producer the information about the people involved in the interview and important information discussed during the interaction using a well-structured JSON",
"input_schema": {
"type": "object",
"description": "Key summary information from an interaction (phone call, in person discussion, or post-conversation summary) with a producer by a technical assistance provider",
"items": {
"type": "object",
"description": "A single summarized interaction",
"properties": {
"people": {
"type": ["array", "null"],
"description": "An array of people involved in or mentioned in this interaction",
"items": {
"type": "object",
"description": "An individual person and their role",
"properties": {
"name": {
"type": "string",
"description": "Name of this person",
},
"role": {
"type": ["string", "null"],
"enum": ["partner", "staff", "agronomist", "other"],
"description": "Role of this person",
}
},
"additionalProperties": False,
"required": [
"name",
"role"
]
}
},
"date": {
"anyOf": [
{
"type": "null"
},
{
"type": "string",
"format": "date",
"description": "Date of the interaction",
},
]
},
"next_meeting": {
"anyOf": [
{
"type": "null"
},
{
"type": "string",
"format": "date",
"description": "Proposed date for the next interaction",
},
]
},
"next_steps": {
"type": ["array", "null"],
"description": "Array containing a list of next steps from the interaction",
"items": {
"type": ["string", "null"],
"description": "An individual next step",
}
},
"summary": {
"type": ["string", "null"],
"description": "A summary of the interaction",
}
},
"additionalProperties": False,
"required": [
"people",
"date",
"next_meeting",
"next_steps",
"summary"
]
}
}
},
{
"name": "plantings_and_fields_schema",
"description": "Record from an interview transcript of an interaction between a Technical Assistance Provider (TAP) and a producer the information regarding the crops planted, information regarding the fields the crops were planted on, and information regarding the activities that took place on the fields using a well-structured JSON",
"input_schema": {
"type": "object",
"description": "An array of the producer's fields and their plantings. Also includes the list of logs which are associated with those plantings",
"items": {
"type": "object",
"description": "An individual field",
"properties": {
"name": {
"type": "string",
"description": "The name of the field",
},
"description": {
"type": ["string", "null"],
"description": "The description of the field",
},
"plantings": {
"type": "array",
"description": "An array of all the plantings which have occurred on this field",
"items": {
"type": "object",
"description": "An individual planting",
"properties": {
"name": {
"type": ["string", "null"],
"description": "The name of the planting",
},
"status": {
"anyOf": [
{
"type": "null"
},
{
"type": "string",
"enum": ["active", "archived"],
"description": "The status of the planting. \"active\" is a planting which is currently still in the field. \"archived\" is a planting which is no longer in the field (has been terminated or harvested)",
},
]
},
"crop": {
"type": "array",
"items": {
"type": ["string", "null"],
"description": "An array of the crops in this planting",
}
},
"variety": {
"type": "array",
"items": {
"type": ["string", "null"],
"description": "An array of the varieties in this planting",
}
},
"logs": {
"type": ["array", "null"],
"description": "An array of all logs that are associated with this individual planting",
"items": {
"type": "object",
"description": "An individual log",
"properties": {
"convention": {
"anyOf": [
{
"type": "null"
},
{
"type": "string",
"enum": ["log--activity", "log--observation", "log--activity--flaming", "log--activity--grazing", "log--activity--mowing", "log--activity--solarization", "log--activity--termination", "log--activity--tillage", "log--harvest--harvest", "log--input--herbicide_or_pesticide", "log--input--irrigation", "log--input--lime", "log--input--organic_matter", "log--input--seed_treatment", "log--input--seedling_treatment", "log--lab_test--modus_lab_test", "log--seeding--seeding", "log--transplanting--transplanting"],
"description": "This log's convention (ie this log's category or type)",
},
]
},
"date": {
"anyOf": [
{
"type": "null"
},
{
"type": "string",
"format": "date",
"description": "The date of this log",
},
]
},
"description": {
"type": ["string", "null"],
"description": "A description of the details of the log",
}
},
"additionalProperties": False,
"required": [
"convention",
"description",
"date"
]
}
},
"soil": {
"type": ["object", "null"],
"description": "Information about the soil associated with or observed during the time of this planting",
"properties": {
"description": {
"type": ["string", "null"],
"description": "A general description of the soil",
},
"structure": {
"anyOf": [
{
"type": "null"
},
{
"type": "array",
"items": {
"type": "string",
"enum": ["clay", "sandy clay", "silty clay", "sandy clay loam", "silty clay loam", "clay loam", "sandy loam", "silt loam", "loam", "loamy sand", "sand", "silt"],
"description": "The structure of the soil using options from the major soil texture classes (sand, clay, silt)"
}
}
]
},
"biology": {
"type": ["string", "null"],
"description": "Biological activity levels of the soil, including fluffiness, worms and bugs, and other evidence of soil biological activity",
}
},
"additionalProperties": False,
"required": [
"description",
"structure",
"biology"
]
},
"yield": {
"type": ["object", "null"],
"properties": {
"quantity": {
"type": ["string", "null"],
"description": "A description of the the total yield (harvested amount) from this planting, including units when available",
},
"quality": {
"type": ["string", "null"],
"description": "The product quality of the harvest. For example, small or large fruits, sweet or tart flavor, easily molding or containing mold, high number of product seconds, etc.",
}
},
"additionalProperties": False,
"required": [
"quantity",
"quality"
]
}
},
"additionalProperties": False,
"required": [
"name",
"status",
"crop",
"variety",
"logs",
"soil",
"yield"
]
}
}
},
"additionalProperties": False,
"required": [
"name",
"description",
"plantings"
]
}
}
},
{
"name": "trials_and_treatments_schema",
"description": "Record from an interview transcript of an interaction between a Technical Assistance Provider (TAP) and a producer the information surrounding the scientific experiment that was performed using a well-structured JSON",
"input_schema": {
"type": "object",
"description": "An array of on-farm trials (ie experiments) performed by producers with support from staff, agronomists and others",
"items": {
"type": "object",
"description": "An individual trial",
"properties": {
"name": {
"type": ["string", "null"],
"description": "The name of this trial"
},
"description": {
"type": ["string", "null"],
"description": "A description of this trial"
},
"treatments": {
"type": "array",
"description": "An array of different treatments (strips or blocks with the same conditions applied) performed by the partner",
"items": {
"type": "object",
"description": "An individual treatment",
"properties": {
"name": {
"type": ["string", "null"],
"description": "The treatment name"
},
"description": {
"type": ["string", "null"],
"description": "The treatment description, including the conditions within this treatment"
},
"crops": {
"type": ["array", "null"],
"description": "An array of crops being tested in this treatment",
"items": {
"type": "string",
"description": "An individual crop"
}
},
"fields": {
"type": ["array", "null"],
"description": "An array of fields in which this treatment has occurred or will occur",
"items": {
"type": "string",
"description": "An individual field where this treatment has or will occur"
}
}
},
"additionalProperties": False,
"required": [
"name",
"description",
"crops",
"fields"
]
}
},
"learnings": {
"type": ["array", "null"],
"description": "An array of lessons learned from this experiment",
"items": {
"type": "string",
"description": "An individual lesson learned"
}
},
"variables": {
"type": "object",
"description": "Variables (ie factors) in this experiment. Some variables are constant (controlled) and some will vary in order to learn something (independent)",
"properties": {
"controlled": {
"type": ["array", "null"],
"description": "An array of controlled variables, which will be constant (controlled) across all trials",
"items": {
"type": "string",
"description": "An individual controlled variable, meaning a variable which is constant across all trials"
}
},
"independent": {
"type": ["array", "null"],
"description": "An array of independent variables (ie treatments), which will be intentionally varied across one or more trials",
"items": {
"type": "string",
"description": "An individual independent variable (ie treatments), meaning a variable which will be intentionally varied across one or more trials"
}
},
"outcome": {
"type": ["array", "null"],
"description": "An array of outcome variables (ie dependent or response variables) which is hypothesized to change across the trials",
"items": {
"type": "string",
"description": "An individual outcome variable (ie dependent or response variables)"
}
}
},
"additionalProperties": False,
"required": [
"controlled",
"independent",
"outcome"
]
},
"confounding_factors": {
"type": ["array", "null"],
"description": "An array of factors which may impact the outcomes of the experiment that were not planned for.",
"items": {
"type": "string",
"description": "An individual confounding factor"
}
}
},
"additionalProperties": False,
"required": [
"name",
"description",
"treatments",
"learnings",
"variables",
"confounding_factors"
]
}
}
}
]
def respond(message, tool_options, tool_name):
if tool_options == "Force Use Specific Tool":
message = client.messages.create(
model=MODEL_NAME,
max_tokens=4096,
tools=tools,
tool_choice={"type": "tool", "name": tool_name},
messages=[
{
"role": "user",
"content": message
}
]
)
json_response = None
for content in message.content:
if content.type == "tool_use" and content.name == tool_name:
json_response = content.input
break
if json_response:
return json_response
else:
print(f"Could not use {tool_name} on input data")
elif tool_options == "Plain (No Tools)":
message = client.messages.create(
model=MODEL_NAME,
max_tokens=4096,
messages=[
{
"role": "user",
"content": message
},
]
).content[0].text
return message
with gr.Blocks() as demo:
input_textbox = gr.Textbox(label="Input Data:")
output_textbox = gr.Textbox(label="JSON Output:")
submit_button = gr.Button("Create JSONs")
radio_tool_settings = gr.Radio(["Force Use Specific Tool", "Plain (No Tools)"], label="Claude Tool Use Options")
radio_choose_tool = gr.Radio(["plantings_and_fields_schema", "interactions_schema", "trials_and_treatments_schema"], label="Claude Specific Tools Available", visible=False)
def show_tools(radio_tool_settings):
value = radio_tool_settings
if value == "Force Use Specific Tool":
return gr.Textbox(visible=bool(1))
else:
return gr.Textbox(visible=bool(0))
radio_tool_settings.change(show_tools, radio_tool_settings, radio_choose_tool)
submit_button.click(
fn=respond,
inputs=[input_textbox, radio_tool_settings, radio_choose_tool],
outputs=output_textbox
)
if __name__ == "__main__":
demo.launch()
|