import Portkey from "portkey-ai"; let portkey = new Portkey({ apiKey: process.env.PORTKEYAI_API_KEY, virtualKey: process.env.TOGETHERAI_API_KEY, }); try { var response = await portkey.chat.completions.create({ messages, model: "togethercomputer/llama-2-70b-chat", }); console.info("Success", response); } catch (error) { console.error("We saw errors getting response from LLM", error); }
error: Either x-portkey-config or x-portkey-provider header is required at new APIError (/Users/saifas/PKey/cookbook/scripts/node_modules/portkey-ai/dist/src/error.js:7:8) at new BadRequestError (/Users/saifas/PKey/cookbook/scripts/node_modules/portkey-ai/dist/src/error.js:78:8) at generate (/Users/saifas/PKey/cookbook/scripts/node_modules/portkey-ai/dist/src/error.js:25:19) at /Users/saifas/PKey/cookbook/scripts/node_modules/portkey-ai/dist/src/baseClient.js:118:22 at fulfilled (/Users/saifas/PKey/cookbook/scripts/node_modules/portkey-ai/dist/src/baseClient.js:5:47)
curl --request POST \ --url https://api.portkey.ai/v1/chat/completions \ --header 'Content-Type: application/json' \ --header 'x-portkey-api-key: sxxx=' \ --header 'x-portkey-config: {"retry":{"attempts":3},"cache":{"mode":"simple"},"strategy":{"mode":"loadbalance"},"targets":[{"virtual_key":"open-ai-kxey-","weight":0.7},{"virtual_key":"test-virtual-ke","weight":0.3}]}' \ --header 'x-portkey-virtual-key: open-ai-key' \ --data '{ "model": "gpt-3.5-turbo", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ] }'
x-portkey-virtual-key
will be ignored since x-portkey-config
enabled.model
is mandatory property, and gpt-3.5-turbo
is chosen model, do loadbalace targets should be same gpt-3.5-turbo
models with different virtual keys? I suppose I am getting this bit wrong in my understanding?ngrok
and llama3
running in the local, when I try to run chat completions call using Portkey, I see following response:bun ollama-llama3.js reaching llama3 { provider: "ollama", getHeaders: [Function: getHeaders], }
const portkey = new Portkey({ apiKey: process.env.PORTKEY_API_KEY, provider: 'ollama', customHost: 'https://6b73-165-1-160-105.ngrok-free.app ', traceID: 'ollama-llama3' }); console.log('reaching llama3'); const chatCompletion = await portkey.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'llama3' }); console.log(chatCompletion);
const portkey = new Portkey({ apiKey: PORTKEY_API_KEY, config: { strategy: { mode: 'fallback' }, targets: [ { promptID: 'pp-test-811461' }, { promptID: 'pp-l-i-ef463c' } ] } });
const response = await portkey.prompts.completions.create({ variables: { city: 'Hyderabad', name: 'Nobita' } }); console.log(response.choices);
response_format
to be url
. config_json = json.dumps(config) url = 'https://api.portkey.ai/v1/images/generations' headers = { 'Content-Type': 'application/json', 'x-portkey-api-key': f'{PORTKEY_API_KEY}', } headers['x-portkey-config'] = config_json data = {"prompt": "Harry potter using aeroplane for transport", "response_format":"url"} response = requests.post(url, headers=headers, data=json.dumps(data)) generation_response = response.json() print(generation_response) # {'created': '1710664648966', 'data': [{'b64_json': 'iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACA....
Accept: image/png
). But it does return bytes
. How can one get them through Portkey ?togethercomputer/
to list the full model name.import Portkey from 'portkey-ai' const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", config: "pc-blog-o-0e83d2" // replace with your config ID }) // We can also override the hyperparameters const pcompletion = await portkey.prompts.completions.create({ promptID: "pp-blog-outli-840877", // Use any prompt ID variables: { "title": "Should colleges permit the use of AI in assignments?", "num_sections": "5" }, }); console.log(pcompletion.choices)
promptID
given these are part of config already?claude-2.1
through portkey. It is clear that I missed the adding a required parameters to consume claude-2.1
, which is max_tokens
. However, I was expecting the error to be shown the terminal. But rather I see the error in the console. Is this an expected behaviour?{ "strategy": { "mode": "fallback" }, "targets": [ { "virtual_key": "openai-virtual-key" }, { "virtual_key": "anthropic-virtual-key", "override_params": { "model": "claude-1" } } ] }
The Fallback feature allows you to specify a list of Language Model APIs (LLMs) in a prioritized order. If the primary LLM fails to respond or encounters an error, Portkey will automatically fallback to the next LLM in the list, ensuring your application's robustness and reliability.At https://portkey.ai/docs/product/ai-gateway-streamline-llm-integrations/fallbacks
or encounters an errorShould I assume any other response code outside of 2XX will trigger fallbacks?
override params
in the following snippet:{ "strategy": { "mode": "fallback", }, "targets": [ { "virtualKey": "openai-virtual-key", }, { "virtualKey": "anthropic-virtual-key", "override_params": { "model": "claude-1" } } ] }
import { Portkey, createHeaders } from "portkey-ai"; let reqHeaders = createHeaders({ "cache-force-refresh": true, }); // rest try { var response = await portkey.chat.completions.create( { messages, model: "gpt-3.5-turbo", stream: true, }, { headers: reqHeaders } ); for await (const chunk of response) { process.stdout.write(chunk.choices[0]?.delta?.content || ""); } } catch (error) { console.error("Errors usually happen:", error); }
"headers": { "user-agent": "Bun/1.0.1", "x-forwarded-proto": "https", "x-portkey-headers": "{\"x-portkey-cache-force-refresh\":true}", },
tools
to specify a function specification and get a structured function argument. I also read that we can select a tool_choice
in the consequent calls to LLM. tools
you've defined forever?