Welcome to Portkey Forum

Updated 7 months ago

Pythin client.files.create() functionality with configs and virtual key

Hey confirming that on Pythin client.files.create() is working with Configs as well as Virtual key. Can you share the code snippet you're using? Can quickly see if something needs to be fixed there
S
V
7 comments
Sure,

Plain Text
client = OpenAI(
    api_key=userdata.get('OPENAI_API_KEY'),
    base_url=PORTKEY_GATEWAY_URL,
    default_headers=createHeaders(
        provider="openai",
     api_key=userdata.get('PORTKEY_API_KEY_2'),
        config="pc-assist-f3c8b9"
    )
)

file = client.files.create(file=open("prompt-partials.pdf", "rb"),purpose="assistants")

file_id = file.id

print(file_id)

Error I see:
Plain Text
APIStatusError                            Traceback (most recent call last)
<ipython-input-30-b038bb4b1448> in <cell line: 1>()
----> 1 client.files.create(
      2   file=open("document.pdf", "rb"),
      3   purpose="fine-tune"
      4 )
      5 

3 frames
/usr/local/lib/python3.10/dist-packages/openai/_base_client.py in _request(self, cast_to, options, remaining_retries, stream, stream_cls)
   1010 
   1011             log.debug("Re-raising status error")
-> 1012             raise self._make_status_error_from_response(err.response) from None
   1013 
   1014         return self._process_response(

APIStatusError: Error code: 415 - {'error': {'message': "Invalid Content-Type header (application/json), expected multipart/form-data. (HINT: If you're using curl, you can pass -H 'Content-Type: multipart/form-data')", 'type': 'invalid_request_error', 'param': None, 'code': None}}
This is because you’re sending a PDF file
Try with a JSONL, it should work
When the purpose is fine-tune then only JSONL is supported, understandable. However, trying with purpose assistants, (should ideally accept pdf files too) also throws APIStatusError.
What's in your Config? 🧐
I picked semantic caching randomly:
Plain Text
"cache": { "mode": "semantic" }
Unsure why this isn't working. :/ Have you updated the OpenAI package to latest version?

Here's the code that should work:

Plain Text
from openai import OpenAI
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

client = OpenAI(
    base_url=PORTKEY_GATEWAY_URL,
    default_headers=createHeaders(
        api_key="PORTKEY_API_KEY",
        virtual_key="OPENAI_VIRTUAL_KEY",
        config="CONFIG_ID"
    )
)

file = client.files.create(
  file=open("mistral.pdf", "rb"),
  purpose="assistants"
)

file_id = file.id

print(file_id)
Add a reply
Sign up and join the conversation on Discord