|
| 1 | +from langtrace_python_sdk import langtrace |
| 2 | +from cerebras.cloud.sdk import Cerebras |
| 3 | +from dotenv import load_dotenv |
| 4 | +import re |
| 5 | +import json |
| 6 | +from openai import OpenAI |
| 7 | +import os |
| 8 | + |
| 9 | +load_dotenv() |
| 10 | + |
| 11 | +langtrace.init() |
| 12 | +openai_client = OpenAI( |
| 13 | + base_url="https://api.cerebras.ai/v1", |
| 14 | + api_key=os.getenv("CEREBRAS_API_KEY"), |
| 15 | +) |
| 16 | +client = Cerebras() |
| 17 | + |
| 18 | + |
| 19 | +def openai_cerebras_example(stream=False): |
| 20 | + completion = openai_client.chat.completions.create( |
| 21 | + messages=[ |
| 22 | + { |
| 23 | + "role": "user", |
| 24 | + "content": "Why is fast inference important?", |
| 25 | + } |
| 26 | + ], |
| 27 | + model="llama3.1-8b", |
| 28 | + stream=stream, |
| 29 | + ) |
| 30 | + |
| 31 | + if stream: |
| 32 | + for chunk in completion: |
| 33 | + print(chunk) |
| 34 | + else: |
| 35 | + return completion |
| 36 | + |
| 37 | + |
| 38 | +def completion_example(stream=False): |
| 39 | + completion = client.chat.completions.create( |
| 40 | + messages=[ |
| 41 | + { |
| 42 | + "role": "user", |
| 43 | + "content": "Why is fast inference important?", |
| 44 | + } |
| 45 | + ], |
| 46 | + model="llama3.1-8b", |
| 47 | + stream=stream, |
| 48 | + ) |
| 49 | + |
| 50 | + if stream: |
| 51 | + for chunk in completion: |
| 52 | + print(chunk) |
| 53 | + else: |
| 54 | + return completion |
| 55 | + |
| 56 | + |
| 57 | +def completion_with_tools_example(stream=False): |
| 58 | + messages = [ |
| 59 | + { |
| 60 | + "role": "system", |
| 61 | + "content": "You are a helpful assistant with access to a calculator. Use the calculator tool to compute mathematical expressions when needed.", |
| 62 | + }, |
| 63 | + {"role": "user", "content": "What's the result of 15 multiplied by 7?"}, |
| 64 | + ] |
| 65 | + |
| 66 | + response = client.chat.completions.create( |
| 67 | + model="llama3.1-8b", |
| 68 | + messages=messages, |
| 69 | + tools=tools, |
| 70 | + stream=stream, |
| 71 | + ) |
| 72 | + |
| 73 | + if stream: |
| 74 | + # Handle streaming response |
| 75 | + full_content = "" |
| 76 | + for chunk in response: |
| 77 | + if chunk.choices[0].delta.tool_calls: |
| 78 | + tool_call = chunk.choices[0].delta.tool_calls[0] |
| 79 | + if hasattr(tool_call, "function"): |
| 80 | + if tool_call.function.name == "calculate": |
| 81 | + arguments = json.loads(tool_call.function.arguments) |
| 82 | + result = calculate(arguments["expression"]) |
| 83 | + print(f"Calculation result: {result}") |
| 84 | + |
| 85 | + # Get final response with calculation result |
| 86 | + messages.append( |
| 87 | + { |
| 88 | + "role": "assistant", |
| 89 | + "content": None, |
| 90 | + "tool_calls": [ |
| 91 | + { |
| 92 | + "function": { |
| 93 | + "name": "calculate", |
| 94 | + "arguments": tool_call.function.arguments, |
| 95 | + }, |
| 96 | + "id": tool_call.id, |
| 97 | + "type": "function", |
| 98 | + } |
| 99 | + ], |
| 100 | + } |
| 101 | + ) |
| 102 | + messages.append( |
| 103 | + { |
| 104 | + "role": "tool", |
| 105 | + "content": str(result), |
| 106 | + "tool_call_id": tool_call.id, |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + final_response = client.chat.completions.create( |
| 111 | + model="llama3.1-70b", messages=messages, stream=True |
| 112 | + ) |
| 113 | + |
| 114 | + for final_chunk in final_response: |
| 115 | + if final_chunk.choices[0].delta.content: |
| 116 | + print(final_chunk.choices[0].delta.content, end="") |
| 117 | + elif chunk.choices[0].delta.content: |
| 118 | + print(chunk.choices[0].delta.content, end="") |
| 119 | + full_content += chunk.choices[0].delta.content |
| 120 | + else: |
| 121 | + # Handle non-streaming response |
| 122 | + choice = response.choices[0].message |
| 123 | + if choice.tool_calls: |
| 124 | + function_call = choice.tool_calls[0].function |
| 125 | + if function_call.name == "calculate": |
| 126 | + arguments = json.loads(function_call.arguments) |
| 127 | + result = calculate(arguments["expression"]) |
| 128 | + print(f"Calculation result: {result}") |
| 129 | + |
| 130 | + messages.append( |
| 131 | + { |
| 132 | + "role": "assistant", |
| 133 | + "content": None, |
| 134 | + "tool_calls": [ |
| 135 | + { |
| 136 | + "function": { |
| 137 | + "name": "calculate", |
| 138 | + "arguments": function_call.arguments, |
| 139 | + }, |
| 140 | + "id": choice.tool_calls[0].id, |
| 141 | + "type": "function", |
| 142 | + } |
| 143 | + ], |
| 144 | + } |
| 145 | + ) |
| 146 | + messages.append( |
| 147 | + { |
| 148 | + "role": "tool", |
| 149 | + "content": str(result), |
| 150 | + "tool_call_id": choice.tool_calls[0].id, |
| 151 | + } |
| 152 | + ) |
| 153 | + |
| 154 | + final_response = client.chat.completions.create( |
| 155 | + model="llama3.1-70b", |
| 156 | + messages=messages, |
| 157 | + ) |
| 158 | + |
| 159 | + if final_response: |
| 160 | + print(final_response.choices[0].message.content) |
| 161 | + else: |
| 162 | + print("No final response received") |
| 163 | + else: |
| 164 | + print("Unexpected response from the model") |
| 165 | + |
| 166 | + |
| 167 | +def calculate(expression): |
| 168 | + expression = re.sub(r"[^0-9+\-*/().]", "", expression) |
| 169 | + |
| 170 | + try: |
| 171 | + result = eval(expression) |
| 172 | + return str(result) |
| 173 | + except (SyntaxError, ZeroDivisionError, NameError, TypeError, OverflowError): |
| 174 | + return "Error: Invalid expression" |
| 175 | + |
| 176 | + |
| 177 | +tools = [ |
| 178 | + { |
| 179 | + "type": "function", |
| 180 | + "function": { |
| 181 | + "name": "calculate", |
| 182 | + "description": "A calculator tool that can perform basic arithmetic operations. Use this when you need to compute mathematical expressions or solve numerical problems.", |
| 183 | + "parameters": { |
| 184 | + "type": "object", |
| 185 | + "properties": { |
| 186 | + "expression": { |
| 187 | + "type": "string", |
| 188 | + "description": "The mathematical expression to evaluate", |
| 189 | + } |
| 190 | + }, |
| 191 | + "required": ["expression"], |
| 192 | + }, |
| 193 | + }, |
| 194 | + } |
| 195 | +] |
0 commit comments