|
| 1 | +import json |
| 2 | + |
1 | 3 | from dotenv import find_dotenv, load_dotenv |
2 | | -from langchain_core.prompts import ChatPromptTemplate |
3 | | -from langchain_groq import ChatGroq |
4 | 4 | from groq import Groq |
5 | 5 |
|
6 | 6 | _ = load_dotenv(find_dotenv()) |
@@ -30,6 +30,82 @@ def groq_basic(): |
30 | 30 | return chat_completion |
31 | 31 |
|
32 | 32 |
|
| 33 | +def groq_tool_choice(): |
| 34 | + |
| 35 | + user_prompt = "What is 25 * 4 + 10?" |
| 36 | + MODEL = "llama3-groq-70b-8192-tool-use-preview" |
| 37 | + |
| 38 | + def calculate(expression): |
| 39 | + """Evaluate a mathematical expression""" |
| 40 | + try: |
| 41 | + result = eval(expression) |
| 42 | + return json.dumps({"result": result}) |
| 43 | + except: |
| 44 | + return json.dumps({"error": "Invalid expression"}) |
| 45 | + |
| 46 | + messages = [ |
| 47 | + { |
| 48 | + "role": "system", |
| 49 | + "content": "You are a calculator assistant. Use the calculate function to perform mathematical operations and provide the results.", |
| 50 | + }, |
| 51 | + { |
| 52 | + "role": "user", |
| 53 | + "content": user_prompt, |
| 54 | + }, |
| 55 | + ] |
| 56 | + tools = [ |
| 57 | + { |
| 58 | + "type": "function", |
| 59 | + "function": { |
| 60 | + "name": "calculate", |
| 61 | + "description": "Evaluate a mathematical expression", |
| 62 | + "parameters": { |
| 63 | + "type": "object", |
| 64 | + "properties": { |
| 65 | + "expression": { |
| 66 | + "type": "string", |
| 67 | + "description": "The mathematical expression to evaluate", |
| 68 | + } |
| 69 | + }, |
| 70 | + "required": ["expression"], |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + ] |
| 75 | + response = client.chat.completions.create( |
| 76 | + model=MODEL, |
| 77 | + messages=messages, |
| 78 | + tools=tools, |
| 79 | + tool_choice={"type": "function", "function": {"name": "calculate"}}, |
| 80 | + max_tokens=4096, |
| 81 | + ) |
| 82 | + |
| 83 | + response_message = response.choices[0].message |
| 84 | + tool_calls = response_message.tool_calls |
| 85 | + if tool_calls: |
| 86 | + available_functions = { |
| 87 | + "calculate": calculate, |
| 88 | + } |
| 89 | + messages.append(response_message) |
| 90 | + for tool_call in tool_calls: |
| 91 | + function_name = tool_call.function.name |
| 92 | + function_to_call = available_functions[function_name] |
| 93 | + function_args = json.loads(tool_call.function.arguments) |
| 94 | + function_response = function_to_call( |
| 95 | + expression=function_args.get("expression") |
| 96 | + ) |
| 97 | + messages.append( |
| 98 | + { |
| 99 | + "tool_call_id": tool_call.id, |
| 100 | + "role": "tool", |
| 101 | + "name": function_name, |
| 102 | + "content": function_response, |
| 103 | + } |
| 104 | + ) |
| 105 | + second_response = client.chat.completions.create(model=MODEL, messages=messages) |
| 106 | + return second_response.choices[0].message.content |
| 107 | + |
| 108 | + |
33 | 109 | def groq_streaming(): |
34 | 110 | chat_completion = client.chat.completions.create( |
35 | 111 | messages=[ |
|
0 commit comments