|
1 | | -# Edgee Gateway SDK |
| 1 | +# Edgee Python SDK |
2 | 2 |
|
3 | | -Lightweight Python SDK for Edgee AI Gateway with built-in tool execution support. |
| 3 | +Lightweight, type-safe Python SDK for the [Edgee AI Gateway](https://www.edgee.cloud). |
| 4 | + |
| 5 | +[]( ) |
| 6 | +[](LICENSE) |
4 | 7 |
|
5 | 8 | ## Installation |
6 | 9 |
|
7 | 10 | ```bash |
8 | 11 | pip install edgee |
9 | 12 | ``` |
10 | 13 |
|
11 | | -## Usage |
| 14 | +## Quick Start |
12 | 15 |
|
13 | 16 | ```python |
14 | 17 | import os |
15 | 18 | from edgee import Edgee |
16 | 19 |
|
17 | | -edgee = Edgee(os.environ.get("EDGEE_API_KEY")) |
18 | | -``` |
19 | | - |
20 | | -### Simple Input |
| 20 | +edgee = Edgee("your-api-key") |
21 | 21 |
|
22 | | -```python |
| 22 | +# Send a simple request |
23 | 23 | response = edgee.send( |
24 | 24 | model="gpt-4o", |
25 | | - input="What is the capital of France?", |
| 25 | + input="What is the capital of France?" |
26 | 26 | ) |
27 | 27 |
|
28 | 28 | print(response.text) |
| 29 | +# "The capital of France is Paris." |
29 | 30 | ``` |
30 | 31 |
|
31 | | -### Full Input with Messages |
32 | | - |
33 | | -```python |
34 | | -response = edgee.send( |
35 | | - model="gpt-4o", |
36 | | - input={ |
37 | | - "messages": [ |
38 | | - {"role": "system", "content": "You are a helpful assistant."}, |
39 | | - {"role": "user", "content": "Hello!"}, |
40 | | - ], |
41 | | - }, |
42 | | -) |
43 | | -``` |
| 32 | +## Send Method |
44 | 33 |
|
45 | | -## Tools |
46 | | - |
47 | | -The SDK supports two modes for working with tools: |
48 | | - |
49 | | -### Simple Mode: Automatic Tool Execution |
50 | | - |
51 | | -Use the `Tool` class with Pydantic models for automatic tool execution. The SDK will handle the entire agentic loop for you: |
| 34 | +The `send()` method makes non-streaming chat completion requests: |
52 | 35 |
|
53 | 36 | ```python |
54 | | -from pydantic import BaseModel |
55 | | -from edgee import Edgee, Tool |
56 | | - |
57 | | -edgee = Edgee(os.environ.get("EDGEE_API_KEY")) |
58 | | - |
59 | | -# Define tool parameters with Pydantic |
60 | | -class WeatherParams(BaseModel): |
61 | | - location: str |
62 | | - |
63 | | -# Define the tool handler |
64 | | -def get_weather(params: WeatherParams) -> dict: |
65 | | - # Your implementation here |
66 | | - return {"temperature": 22, "condition": "sunny", "location": params.location} |
67 | | - |
68 | | -# Create the tool |
69 | | -weather_tool = Tool( |
70 | | - name="get_weather", |
71 | | - description="Get the current weather for a location", |
72 | | - schema=WeatherParams, |
73 | | - handler=get_weather, |
74 | | -) |
75 | | - |
76 | | -# The SDK automatically: |
77 | | -# 1. Sends the request with tools |
78 | | -# 2. Executes tools when the model requests them |
79 | | -# 3. Sends results back to the model |
80 | | -# 4. Returns the final response |
81 | 37 | response = edgee.send( |
82 | 38 | model="gpt-4o", |
83 | | - input="What's the weather in Paris?", |
84 | | - tools=[weather_tool], |
| 39 | + input="Hello, world!" |
85 | 40 | ) |
86 | 41 |
|
87 | | -print(response.text) |
88 | | -# "The weather in Paris is sunny with a temperature of 22°C." |
| 42 | +# Access response |
| 43 | +print(response.text) # Text content |
| 44 | +print(response.finish_reason) # Finish reason |
| 45 | +print(response.tool_calls) # Tool calls (if any) |
89 | 46 | ``` |
90 | 47 |
|
91 | | -#### Multiple Tools |
92 | | - |
93 | | -```python |
94 | | -from typing import Literal |
95 | | -from pydantic import BaseModel |
96 | | - |
97 | | -class CalculatorParams(BaseModel): |
98 | | - operation: Literal["add", "subtract", "multiply", "divide"] |
99 | | - a: float |
100 | | - b: float |
101 | | - |
102 | | -def calculate(params: CalculatorParams) -> dict: |
103 | | - ops = { |
104 | | - "add": params.a + params.b, |
105 | | - "subtract": params.a - params.b, |
106 | | - "multiply": params.a * params.b, |
107 | | - "divide": params.a / params.b if params.b != 0 else "Error: division by zero", |
108 | | - } |
109 | | - return {"result": ops[params.operation]} |
| 48 | +## Stream Method |
110 | 49 |
|
111 | | -calculator_tool = Tool( |
112 | | - name="calculate", |
113 | | - description="Perform arithmetic operations", |
114 | | - schema=CalculatorParams, |
115 | | - handler=calculate, |
116 | | -) |
117 | | - |
118 | | -response = edgee.send( |
119 | | - model="gpt-4o", |
120 | | - input="What's 25 * 4, and what's the weather in London?", |
121 | | - tools=[weather_tool, calculator_tool], |
122 | | -) |
123 | | -``` |
124 | | - |
125 | | -#### Configuration Options |
| 50 | +The `stream()` method enables real-time streaming responses: |
126 | 51 |
|
127 | 52 | ```python |
128 | | -response = edgee.send( |
129 | | - model="gpt-4o", |
130 | | - input="Complex query requiring multiple tool calls", |
131 | | - tools=[tool1, tool2], |
132 | | - max_tool_iterations=15, # Default: 10 |
133 | | -) |
134 | | -``` |
135 | | - |
136 | | -### Advanced Mode: Manual Tool Handling |
137 | | - |
138 | | -For full control over tool execution, use the advanced mode with raw tool definitions: |
139 | | - |
140 | | -```python |
141 | | -response = edgee.send( |
142 | | - model="gpt-4o", |
143 | | - input={ |
144 | | - "messages": [{"role": "user", "content": "What's the weather in Paris?"}], |
145 | | - "tools": [ |
146 | | - { |
147 | | - "type": "function", |
148 | | - "function": { |
149 | | - "name": "get_weather", |
150 | | - "description": "Get weather for a location", |
151 | | - "parameters": { |
152 | | - "type": "object", |
153 | | - "properties": { |
154 | | - "location": {"type": "string"}, |
155 | | - }, |
156 | | - }, |
157 | | - }, |
158 | | - }, |
159 | | - ], |
160 | | - "tool_choice": "auto", |
161 | | - }, |
162 | | -) |
163 | | - |
164 | | -# Handle tool calls manually |
165 | | -if response.tool_calls: |
166 | | - print(response.tool_calls) |
167 | | - # Execute tools and send results back... |
168 | | -``` |
169 | | - |
170 | | -## Streaming |
171 | | - |
172 | | -```python |
173 | | -for chunk in edgee.stream(model="gpt-4o", input="Tell me a story"): |
| 53 | +for chunk in edgee.stream("gpt-4o", "Tell me a story"): |
174 | 54 | if chunk.text: |
175 | 55 | print(chunk.text, end="", flush=True) |
176 | | -``` |
177 | 56 |
|
178 | | -#### Alternative: Using send(stream=True) |
179 | | - |
180 | | -```python |
181 | | -for chunk in edgee.send(model="gpt-4o", input="Tell me a story", stream=True): |
182 | | - if chunk.text: |
183 | | - print(chunk.text, end="", flush=True) |
| 57 | + if chunk.finish_reason: |
| 58 | + print(f"\nFinished: {chunk.finish_reason}") |
184 | 59 | ``` |
185 | 60 |
|
186 | | -## Response |
| 61 | +## Features |
187 | 62 |
|
188 | | -```python |
189 | | -@dataclass |
190 | | -class SendResponse: |
191 | | - choices: list[Choice] |
192 | | - usage: Usage | None |
| 63 | +- ✅ **Type-safe** - Full type hints with dataclasses |
| 64 | +- ✅ **OpenAI-compatible** - Works with any model supported by Edgee |
| 65 | +- ✅ **Streaming** - Real-time response streaming with generators |
| 66 | +- ✅ **Tool calling** - Full support for function calling |
| 67 | +- ✅ **Flexible input** - Accept strings, dicts, or InputObject |
| 68 | +- ✅ **Zero dependencies** - Uses only Python standard library |
193 | 69 |
|
194 | | - # Convenience properties |
195 | | - text: str | None # choices[0].message["content"] |
196 | | - message: dict | None # choices[0].message |
197 | | - finish_reason: str | None # choices[0].finish_reason |
198 | | - tool_calls: list | None # choices[0].message["tool_calls"] |
| 70 | +## Documentation |
199 | 71 |
|
200 | | -@dataclass |
201 | | -class Choice: |
202 | | - index: int |
203 | | - message: dict # {"role": str, "content": str | None, "tool_calls": list | None} |
204 | | - finish_reason: str | None |
| 72 | +For complete documentation, examples, and API reference, visit: |
205 | 73 |
|
206 | | -@dataclass |
207 | | -class InputTokenDetails: |
208 | | - cached_tokens: int |
| 74 | +**👉 [Official Python SDK Documentation](https://www.edgee.cloud/docs/sdk/python)** |
209 | 75 |
|
210 | | -@dataclass |
211 | | -class OutputTokenDetails: |
212 | | - reasoning_tokens: int |
| 76 | +The documentation includes: |
| 77 | +- [Configuration guide](https://www.edgee.cloud/docs/sdk/python/configuration) - Multiple ways to configure the SDK |
| 78 | +- [Send method](https://www.edgee.cloud/docs/sdk/python/send) - Complete guide to non-streaming requests |
| 79 | +- [Stream method](https://www.edgee.cloud/docs/sdk/python/stream) - Streaming responses guide |
| 80 | +- [Tools](https://www.edgee.cloud/docs/sdk/python/tools) - Function calling guide |
213 | 81 |
|
214 | | -@dataclass |
215 | | -class Usage: |
216 | | - prompt_tokens: int |
217 | | - completion_tokens: int |
218 | | - total_tokens: int |
219 | | - input_tokens_details: InputTokenDetails |
220 | | - output_tokens_details: OutputTokenDetails |
221 | | -``` |
222 | | - |
223 | | -### Streaming Response |
224 | | - |
225 | | -```python |
226 | | -@dataclass |
227 | | -class StreamChunk: |
228 | | - choices: list[StreamChoice] |
| 82 | +## License |
229 | 83 |
|
230 | | - # Convenience properties |
231 | | - text: str | None # choices[0].delta.content |
232 | | - role: str | None # choices[0].delta.role |
233 | | - finish_reason: str | None # choices[0].finish_reason |
234 | | - |
235 | | -@dataclass |
236 | | -class StreamChoice: |
237 | | - index: int |
238 | | - delta: StreamDelta |
239 | | - finish_reason: str | None |
240 | | - |
241 | | -@dataclass |
242 | | -class StreamDelta: |
243 | | - role: str | None |
244 | | - content: str | None |
245 | | - tool_calls: list[dict] | None |
246 | | -``` |
247 | | - |
248 | | -## API Reference |
249 | | - |
250 | | -### `Tool` Class |
251 | | - |
252 | | -```python |
253 | | -from pydantic import BaseModel |
254 | | -from edgee import Tool |
255 | | - |
256 | | -class MyParams(BaseModel): |
257 | | - param1: str |
258 | | - param2: int |
259 | | - |
260 | | -tool = Tool( |
261 | | - name="my_tool", # Unique tool name |
262 | | - description="...", # Tool description for the model |
263 | | - schema=MyParams, # Pydantic model for parameters |
264 | | - handler=my_function, # Function to execute |
265 | | -) |
266 | | - |
267 | | -# Methods |
268 | | -tool.to_dict() # Convert to OpenAI tool format |
269 | | -tool.execute(args) # Validate and execute handler |
270 | | -``` |
271 | | - |
272 | | -### `create_tool` Helper |
273 | | - |
274 | | -```python |
275 | | -from edgee import create_tool |
276 | | - |
277 | | -tool = create_tool( |
278 | | - name="my_tool", |
279 | | - schema=MyParams, |
280 | | - handler=my_function, |
281 | | - description="...", |
282 | | -) |
283 | | -``` |
| 84 | +Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for details. |
0 commit comments