Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider: SambaNova FastAPI implementation. #548

Merged
merged 16 commits into from
Sep 10, 2024

Conversation

ilsubyeega
Copy link
Contributor

This PR implements SambaNova FastAPI implementation. Which is currently private-beta, but the request codes are in sambanova/ai-starter-kit repository. Whole codebases are copied from /src/provider/groq directory.

Related #407. But this does not implement any other apis that are mentioned.

It's working when quick and simple test, but I must verify that it works properly, so I've submitted this as Draft.

@ilsubyeega
Copy link
Contributor Author

I think this is okay for now.

@ilsubyeega ilsubyeega marked this pull request as ready for review September 1, 2024 06:39
@VisargD
Copy link
Collaborator

VisargD commented Sep 1, 2024

Hey! Thanks for the PR. We recently released a openai base provider which can be used for all openai compatible providers. It would be great if you can use it for this provider as well because its OpenAI compliant. You can check the Cerebras integration as a reference on how to use the base provider.

@ilsubyeega
Copy link
Contributor Author

ilsubyeega commented Sep 2, 2024

We recently released a openai base provider which can be used for all openai compatible providers.

@VisargD I'm curious that this open-ai-base provides stream-chatComplete. I can only see that transform non-stream repsonse but not stream one (and even it has custom property at sambanova).[1]

Also, i doubt at

defaultValues?: Record<string, string>,

chatCompleteParams#defaultValues?: Record<string, string> looks wrongly designed. PrarameterConfig.default? provides any type, but #defaultValues only appliable with string but not boolean and numbers. I'm just putting string at this moment, but not ideal i guess.

[1]: Edit; seems groq got reverted, nevermind.

VisargD
VisargD previously approved these changes Sep 2, 2024
src/providers/sambanova/chatComplete.ts Outdated Show resolved Hide resolved
};
index: number;
finish_reason: string | null;
logprobs: object | null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just do Record instead of object here?

src/providers/sambanova/chatComplete.ts Outdated Show resolved Hide resolved
'tools',
'tool_choice',
'response_format',
'logprobs',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're expecting the response to send logprobs but we're excluding it here, is there any issue here?

Sorry, I'm missing the documentation with me, so had to confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, i will review these as soon as possible.

if it meant that there are no logprob params, at this moment, there is no mention of these params. i want to believe the docs that i have dont have complete references.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that the request would have that logprobs arg, but it wasn't documented.

Copy link
Contributor

@b4s36t4 b4s36t4 Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, for now we can remove the logprobs i.e don't expect it to be available in the response and don't accept it in the request body as well. Once they are with good documentation/ available for everyone we can make the change again.

cc: @VisargD

@b4s36t4
Copy link
Contributor

b4s36t4 commented Sep 6, 2024

@ilsubyeega I have tried running the code locally but keep on getting 401, were you able to test this locally if so can you share a video recording of the response.

Following is the curl I have tried.

curl --request POST \
  --url http://localhost:8787/v1/chat/completions \
  --header 'authorization: key' \
  --header 'content-type: application/json' \
  --header 'x-portkey-provider: sambanova' \
  --data '{
  "messages": [
    {
      "role": "user",
      "content": "Hello, world"
    }
  ]
}'

@ilsubyeega
Copy link
Contributor Author

were you able to test this locally if so can you share a video recording of the response.

@b4s36t4 Here you go.

Screencast.from.2024-09-06.20-54-31.webm

@b4s36t4
Copy link
Contributor

b4s36t4 commented Sep 7, 2024

@ilsubyeega Sorry for the delay. But I just have got merged the typing issue for the defaluValues for the base provider. If possible please update the PR to use stream with boolean value instead of string value. Then we can merge it.

@ilsubyeega
Copy link
Contributor Author

ilsubyeega commented Sep 7, 2024

Here are some concerns: Default parameter does not affect isStreamingMode, which requires stream paramter on request even though it should appended as default.

const overrideParams = providerOption?.overrideParams || {};
const params: Params = { ...inputParams, ...overrideParams };
const isStreamingMode = params.stream ? true : false;
let strictOpenAiCompliance = true;
if (requestHeaders[HEADER_KEYS.STRICT_OPEN_AI_COMPLIANCE] === 'false') {
strictOpenAiCompliance = false;
} else if (providerOption.strictOpenAiCompliance === false) {
strictOpenAiCompliance = false;
}
const provider: string = providerOption.provider ?? '';
const hooksManager = c.get('hooksManager');
const hookSpan = hooksManager.createSpan(
params,
provider,
isStreamingMode,
providerOption.beforeRequestHooks || [],
providerOption.afterRequestHooks || [],
null,
fn
);
// Mapping providers to corresponding URLs
const apiConfig: ProviderAPIConfig = Providers[provider].api;
// Attach the body of the request
const transformedRequestBody = transformToProviderRequest(
provider,
params,
inputParams,
fn
);

transformToProviderRequest parses with default provider config, if it done correctly, but it does not affect isStreamingMode, make it fails.

Since stream is not required param, so it does not append stream: true on body.

// If the parameter is not present in the incoming request body but is required, set it to the default value
else if (
paramConfig &&
paramConfig.required &&
paramConfig.default !== undefined
) {
// Set the transformed parameter to the default value
setNestedProperty(
transformedRequest,
paramConfig.param,
paramConfig.default
);
}

Here is possible workarounds:

  1. /src/handlers/handlerUtils.ts#tryPost, it should be reverify after transformToProviderRequest, e.g
    if ((transformedRequestBody as { stream?: boolean })['stream'] == true) isStreamingMode = true;
  2. Two options: add require params on /src/provider/open-ai-base/index.ts#chatCompleteParams, or remove paramConfig.required check to /src/services/transformToProviderRequest.ts#transformToProviderRequestJSON
    stream: {
      param: 'stream',
      ...(defaultValues?.stream && { default: defaultValues.stream, required: true }), // <--
    },
      else if (
        paramConfig &&
        // paramConfig.required && <--- remove this
        paramConfig.default !== undefined
      ) {

it seems default option is useless when required=false, so the last option looks ideal for me.

Since the provider only supports streaming-mode at this moment, which is unusual, so this issue being kinda tricky.
Or we can just ignore this since the client(requesting to gateway) should set stream=true explicitly, otherwise it will break parsing responses.

@b4s36t4
Copy link
Contributor

b4s36t4 commented Sep 7, 2024

@ilsubyeega Sorry, I didn't understand what you're trying to say. If possible can we take this over discord chat. b4s36t4 is my discord hit me up.

@b4s36t4
Copy link
Contributor

b4s36t4 commented Sep 7, 2024

Also sambanova does support non-stream request as well.

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "message": {
        "content": "It seems like you've mentioned a name, \"Mahesh\". Would you like to know more about someone with this name, or perhaps discuss something related to it?"
      }
    }
  ],
  "created": 1725703777,
  "id": "08f08fe8-889a-4cdb-b406-e4f8077b2537",
  "model": "Meta-Llama-3.1-8B-Instruct",
  "object": "chat.completion",
  "system_fingerprint": "fastcoe",
  "usage": {
    "completion_tokens": 33,
    "completion_tokens_after_first_per_sec": 483.3644057419852,
    "completion_tokens_after_first_per_sec_first_ten": 889.0098453777595,
    "completion_tokens_per_sec": 355.31535860884566,
    "end_time": 1725703777.6294942,
    "is_last_response": true,
    "prompt_tokens": 11,
    "start_time": 1725703777.536619,
    "time_to_first_token": 0.0266726016998291,
    "total_latency": 0.09287524223327637,
    "total_tokens": 44,
    "total_tokens_per_sec": 473.7538114784609
  }
}

@ilsubyeega
Copy link
Contributor Author

Uh... While testing i got nonstream response so I thought that the gateway caching it, but was sambanova side, it works sometimes and sometimes not.

image
image

Copy link
Contributor

@b4s36t4 b4s36t4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @ilsubyeega. Sorry to drag this longer, but while testing I have observed an issue where the provider is sending more than one chunk in the response for stream which is throwing error and not working as expected.

To resolve this you need update the function getStreamModeSplitPattern to support the sambanova. Currently we have different split patterns for different providers, for sambanova we should be adding \n as the separator. Please let me know if there's anything we can discuss over discord.

@VisargD VisargD merged commit 08048e9 into Portkey-AI:main Sep 10, 2024
1 check passed
@vrushankportkey
Copy link
Collaborator

Hey @ilsubyeega - can you please share the email/name of person from Sambanova you were in touch with? We can reach out to to inform about this integration and discuss what more can be done.

@ilsubyeega
Copy link
Contributor Author

Hey @ilsubyeega - can you please share the email/name of person from Sambanova you were in touch with? We can reach out to to inform about this integration and discuss what more can be done.

I am just a private beta test api user and have no connection with sambanova. I received the mail through salesforce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants