Skip to content

Instantly share code, notes, and snippets.

@elijahbenizzy
Last active September 27, 2024 23:48
Show Gist options
  • Save elijahbenizzy/55c0bd29eab2323db6fc6d21a67007a4 to your computer and use it in GitHub Desktop.
Save elijahbenizzy/55c0bd29eab2323db6fc6d21a67007a4 to your computer and use it in GitHub Desktop.
@action(reads=["query"], writes=["tool_parameters", "tool"])
def select_tool(state: State) -> State:
messages = [
{
"role": "system",
"content": (
"You are a helpful assistant. Use the supplied tools to assist the user",
", if they apply in any way. Remember to use the tools!",
" They can do stuff you can't.",
"If you can't use only the tools provided to answer the question",
" but know the answer, please provide the answer",
"If you cannot use the tools provided to answer the question",
", use the fallback tool and provide a reason. ",
"Again, if you can't use one tool provided to answer the question",
", use the fallback tool and provide a reason. ",
"You must select exactly one tool no matter what, filling in every",
"parameters with your best guess. Do not skip out on parameters!"
),
},
{"role": "user", "content": state["query"]},
]
response = openai.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=OPENAI_TOOLS,
)
# Extract the tool name and parameters from OpenAI's response
if response.choices[0].message.tool_calls is None:
return state.update(
tool="fallback",
tool_parameters={
"response": ("No tool was selected, instead response was"
f": {response.choices[0].message}.")
},
)
fn = response.choices[0].message.tool_calls[0].function
return state.update(tool=fn.name, tool_parameters=json.loads(fn.arguments))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment