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()