Package: edgen
Edgen Python API libraryThe Edgen Python is a clone of the official OpenAI Python library.
It provides convenient access to the Edgen REST API from any Python 3.7+
application. The library includes type definitions for all request params and response fields,
and offers both synchronous and asynchronous clients powered byhttpx.The original library was generated from theOpenAPI specificationwithStainless.DocumentationThe API documentation can be foundhere.InstallationpipinstalledgenUsageThe full API of this library can be found inapi.md.importosfromedgenimportEdgenclient=Edgen(base_url=os.environ.get("EDGEN_BASE_URL"),# this is default and can be omitted)chat_completion=client.chat.completions.create(messages=[{"role":"user","content":"Say this is a test",}],model="ignore",)Note that, in the current version, the model is ignored.
The model per endpoint is chosen in theedgen-server.
However, themodelelement must be present for compatibility with the OpenAI spec.Async usageSimply importAsyncEdgeninstead ofEdgenand useawaitwith each API call:importosimportasynciofromedgenimportAsyncEdgenclient=AsyncEdgen()asyncdefmain()->None:chat_completion=awaitclient.chat.completions.create(messages=[{"role":"user","content":"Say this is a test",}],model="ignore",)asyncio.run(main())Functionality between the synchronous and asynchronous clients is otherwise identical.Streaming ResponsesWe provide support for streaming responses using Server Side Events (SSE).fromedgenimportEdgenclient=Edgen()stream=client.chat.completions.create(model="ignore",messages=[{"role":"user","content":"Say this is a test"}],stream=True,)forchunkinstream:print(chunk.choices[0].delta.contentor"",end="")The async client uses the exact same interface.fromedgenimportAsyncEdgenclient=AsyncEdgen()asyncdefmain():stream=awaitclient.chat.completions.create(model="ignore",messages=[{"role":"user","content":"Say this is a test"}],stream=True,)asyncforchunkinstream:print(chunk.choices[0].delta.contentor"",end="")asyncio.run(main())Using typesNested request parameters areTypedDicts. Responses arePydantic models, which provide helper methods for things like:Serializing back into JSON,model.model_dump_json(indent=2, exclude_unset=True)Converting to a dictionary,model.model_dump(exclude_unset=True)Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, setpython.analysis.typeCheckingModetobasic.Removeawaitfor non-async usage.## Nested params

Nested parameters are dictionaries, typed using `TypedDict`, for example:

```python
from edgen import Edgen

client = Edgen()

completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Can you generate an example json object describing a fruit?",
        }
    ],
    model="ignore",
    response_format={"type": "json_object"},
)Handling errorsWhen the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass ofedgen.APIConnectionErroris raised.When the API returns a non-success status code (that is, 4xx or 5xx
response), a subclass ofedgen.APIStatusErroris raised, containingstatus_codeandresponseproperties.All errors inherit fromedgen.APIError.importedgenfromedgenimportEdgenclient=Edgen()try:chat_completion=awaitclient.chat.completions.create(messages=[{"role":"user","content":"Say this is a test",}],model="ignore",)exceptedgen.APIConnectionErrorase:print("The server could not be reached")print(e.__cause__)# an underlying Exception, likely raised within httpx.exceptedgen.RateLimitErrorase:print("A 429 status code was received; we should back off a bit.")exceptedgen.APIStatusErrorase:print("Another non-200-range status code was received")print(e.status_code)print(e.response)Error codes are as followed:Status CodeError Type400BadRequestError401AuthenticationError403PermissionDeniedError404NotFoundError422UnprocessableEntityError429RateLimitError>=500InternalServerErrorN/AAPIConnectionErrorRetriesCertain errors are automatically retried 2 times by default, with a short exponential backoff.
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
429 Rate Limit, and >=500 Internal errors are all retried by default.You can use themax_retriesoption to configure or disable retry settings:fromedgenimportEdgen# Configure the default for all requests:client=Edgen(# default is 2max_retries=0,)# Or, configure per-request:client.with_options(max_retries=5).chat.completions.create(messages=[{"role":"user","content":"How can I get the name of the current day in Node.js?",}],model="ignore",)TimeoutsBy default requests time out after 10 minutes. You can configure this with atimeoutoption,
which accepts a float or anhttpx.Timeoutobject:fromedgenimportEdgen# Configure the default for all requests:client=Edgen(# 20 seconds (default is 10 minutes)timeout=20.0,)# More granular control:client=Edgen(timeout=httpx.Timeout(60.0,read=5.0,write=10.0,connect=2.0),)# Override per-request:client.with_options(timeout=5*1000).chat.completions.create(messages=[{"role":"user","content":"How can I list all files in a directory using Python?",}],model="gpt-3.5-turbo",)On timeout, anAPITimeoutErroris thrown.Note that requests that time out areretried twice by default.AdvancedLoggingWe use the standard libraryloggingmodule.You can enable logging by setting the environment variableEDGEN_LOGtodebug.$exportEDGEN_LOG=debugHow to tell whetherNonemeansnullor missingIn an API response, a field may be explicitlynull, or missing entirely; in either case, its value isNonein this library. You can differentiate the two cases with.model_fields_set:ifresponse.my_fieldisNone:if'my_field'notinresponse.model_fields_set:print('Got json like{}, without a "my_field" key present at all.')else:print('Got json like {"my_field": null}.')Accessing raw response data (e.g. headers)The "raw" Response object can be accessed by prefixing.with_raw_response.to any HTTP method call.fromedgenimportEdgenclient=Edgen()response=client.chat.completions.with_raw_response.create(messages=[{"role":"user","content":"Say this is a test",}],model="ignore",)print(response.headers.get('X-My-Header'))completion=response.parse()# get the object that `chat.completions.create()` would have returnedprint(completion)These methods return anAPIResponseobject.Configuring the HTTP clientYou can directly override thehttpx clientto customize it for your use case, including:Support for proxiesCustom transportsAdditionaladvancedfunctionalityimporthttpxfromedgenimportEdgenclient=Edgen(# Or use the `EDGEN_BASE_URL` env varbase_url="http://my.test.server.example.com:8083",http_client=httpx.Client(proxies="http://my.test.proxy.example.com",transport=httpx.HTTPTransport(local_address="0.0.0.0"),),)VersioningThis package generally followsSemVerconventions.RequirementsPython 3.7 or higher.TestsThe tests are based onpytestwith thepytest-asyncio,anyio,Fakerandrespxplugins.