Skip to content

Commit

Permalink
Merge branch 'main' into readme
Browse files Browse the repository at this point in the history
  • Loading branch information
noble-varghese authored Oct 10, 2023
2 parents c72bfcd + 4bf49c3 commit 8cbf28e
Show file tree
Hide file tree
Showing 20 changed files with 1,661 additions and 64 deletions.
23 changes: 21 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
### Changelog
#### Unreleased

All notable changes to this project will be documented in this file. Dates are displayed in UTC.
#### [v0.1.51](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.51...v0.1.52)
> 21 September 2023
- feat: added openschema for all integrations + anyscale integration

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
#### [v0.1.50](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.50...v0.1.51)
> 21 September 2023
- fix: Issue with the str comparison on the api path

#### [v0.1.50](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.49...v0.1.50)
> 19 September 2023
- feat: introducing a "Generate" API for prompt-based content generation with saved prompts

<!-- auto-changelog-below -->

#### [v0.1.49](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.48...v0.1.49)

> 16 September 2023
- fix: Fixing the issue with the capturing of env variables [`#13`](https://github.com/Portkey-AI/portkey-python-sdk/pull/13)

#### [v0.1.48](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.45...v0.1.48)

> 14 September 2023
- fix: cache age type fix [`#11`](https://github.com/Portkey-AI/portkey-python-sdk/pull/11)
- docs: updated change log [`#10`](https://github.com/Portkey-AI/portkey-python-sdk/pull/10)

#### [v0.1.45](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.44...v0.1.45)

Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ You can find a comprehensive [list of Portkey features here](#📔-list-of-portk
Portkey is designed to be flexible. All the features you're familiar with from your LLM provider, like `top_p`, `top_k`, and `temperature`, can be used seamlessly. Check out the [complete list of provider features here](https://github.com/Portkey-AI/portkey-python-sdk/blob/af0814ebf4f1961b5dfed438918fe68b26ef5f1e/portkey/api_resources/utils.py#L137).

**Setting the Prompt Input**:
You can set the input in two ways. For models like Claude and GPT3, use `prompt` = `(str)`, and for models like GPT3.5 & GPT4, use `messages` = `[array]`.
This param lets you override any prompt that is passed during the completion call - set a model-specific prompt here to optimise the model performance. You can set the input in two ways. For models like Claude and GPT3, use `prompt` = `(str)`, and for models like GPT3.5 & GPT4, use `messages` = `[array]`.


Here's how you can combine everything:

Expand All @@ -73,7 +74,7 @@ temperature = 1
messages = [{"role": "user", "content": "Who are you?"}]

# Construct LLM
llm = LLMOptions(provider=provider, virtual_key=virtual_key, trace_id=trace_id, model=model, temperature=temperature, messages=messages)
llm = LLMOptions(provider=provider, virtual_key=virtual_key, trace_id=trace_id, model=model, temperature=temperature)
```

### **Steo 3️⃣ : Construct the Portkey Client**
Expand Down Expand Up @@ -101,8 +102,12 @@ The Portkey client can do `ChatCompletions` and `Completions`.
Since our LLM is GPT4, we will use ChatCompletions:

```py
response = portkey.ChatCompletions.create()

response = portkey.ChatCompletions.create(
messages=[{
"role": "user",
"content": "Who are you ?"
}]
)
print(response.choices[0].message)
```

Expand Down
2 changes: 2 additions & 0 deletions portkey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ChatCompletionChunk,
TextCompletion,
TextCompletionChunk,
Generations,
)
from portkey.version import VERSION
from portkey.api_resources.global_constants import (
Expand Down Expand Up @@ -50,6 +51,7 @@
"ChatCompletionChunk",
"TextCompletion",
"TextCompletionChunk",
"Generations",
"Config",
"api_key",
"base_url",
Expand Down
3 changes: 2 additions & 1 deletion portkey/api_resources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""""""
from .apis import ChatCompletions, Completions
from .apis import ChatCompletions, Completions, Generations
from .utils import (
Modes,
ModesLiteral,
Expand Down Expand Up @@ -41,4 +41,5 @@
"ChatCompletionChunk",
"TextCompletion",
"TextCompletionChunk",
"Generations",
]
54 changes: 36 additions & 18 deletions portkey/api_resources/apis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union, overload, Literal, List
from typing import Optional, Union, overload, Literal, List, Mapping, Any
from portkey.api_resources.base_client import APIClient
from .utils import (
Modes,
Expand All @@ -10,7 +10,7 @@
ChatCompletion,
TextCompletion,
TextCompletionChunk,
ApiType,
GenericResponse,
)

from .streaming import Stream
Expand Down Expand Up @@ -49,7 +49,7 @@ def create(
max_tokens: Optional[int] = None,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
**kwargs
**kwargs,
) -> Stream[TextCompletionChunk]:
...

Expand All @@ -65,7 +65,7 @@ def create(
max_tokens: Optional[int] = None,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
**kwargs
**kwargs,
) -> TextCompletion:
...

Expand All @@ -81,7 +81,7 @@ def create(
max_tokens: Optional[int] = None,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
**kwargs
**kwargs,
) -> Union[TextCompletion, Stream[TextCompletionChunk]]:
...

Expand All @@ -96,7 +96,7 @@ def create(
max_tokens: Optional[int] = None,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
**kwargs
**kwargs,
) -> Union[TextCompletion, Stream[TextCompletionChunk]]:
if config is None:
config = retrieve_config()
Expand All @@ -107,7 +107,7 @@ def create(
max_tokens=max_tokens,
top_k=top_k,
top_p=top_p,
**kwargs
**kwargs,
)
if config.mode == Modes.SINGLE.value:
return cls(_client)._post(
Expand All @@ -118,7 +118,6 @@ def create(
cast_to=TextCompletion,
stream_cls=Stream[TextCompletionChunk],
stream=stream,
_type=ApiType.COMPLETIONS,
)
if config.mode == Modes.FALLBACK.value:
return cls(_client)._post(
Expand All @@ -129,7 +128,6 @@ def create(
cast_to=TextCompletion,
stream_cls=Stream[TextCompletionChunk],
stream=stream,
_type=ApiType.COMPLETIONS,
)
if config.mode == Modes.AB_TEST.value:
return cls(_client)._post(
Expand All @@ -140,7 +138,6 @@ def create(
cast_to=TextCompletion,
stream_cls=Stream[TextCompletionChunk],
stream=stream,
_type=ApiType.COMPLETIONS,
)
raise NotImplementedError("Mode not implemented.")

Expand All @@ -158,7 +155,7 @@ def create(
max_tokens: Optional[int] = None,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
**kwargs
**kwargs,
) -> Stream[ChatCompletionChunk]:
...

Expand All @@ -174,7 +171,7 @@ def create(
max_tokens: Optional[int] = None,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
**kwargs
**kwargs,
) -> ChatCompletion:
...

Expand All @@ -190,7 +187,7 @@ def create(
max_tokens: Optional[int] = None,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
**kwargs
**kwargs,
) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
...

Expand All @@ -205,7 +202,7 @@ def create(
max_tokens: Optional[int] = None,
top_k: Optional[int] = None,
top_p: Optional[float] = None,
**kwargs
**kwargs,
) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
if config is None:
config = retrieve_config()
Expand All @@ -216,7 +213,7 @@ def create(
max_tokens=max_tokens,
top_k=top_k,
top_p=top_p,
**kwargs
**kwargs,
)
if config.mode == Modes.SINGLE.value:
return cls(_client)._post(
Expand All @@ -227,7 +224,6 @@ def create(
cast_to=ChatCompletion,
stream_cls=Stream[ChatCompletionChunk],
stream=stream,
_type=ApiType.CHAT_COMPLETION,
)
if config.mode == Modes.FALLBACK.value:
return cls(_client)._post(
Expand All @@ -238,7 +234,6 @@ def create(
cast_to=ChatCompletion,
stream_cls=Stream[ChatCompletionChunk],
stream=stream,
_type=ApiType.CHAT_COMPLETION,
)
if config.mode == Modes.AB_TEST.value:
return cls(_client)._post(
Expand All @@ -249,6 +244,29 @@ def create(
cast_to=ChatCompletion,
stream_cls=Stream[ChatCompletionChunk],
stream=stream,
_type=ApiType.CHAT_COMPLETION,
)
raise NotImplementedError("Mode not implemented.")


class Generations(APIResource):
@classmethod
def create(
cls,
*,
prompt_id: str,
config: Optional[Config] = None,
variables: Optional[Mapping[str, Any]] = None,
) -> Union[GenericResponse, Stream[GenericResponse]]:
if config is None:
config = retrieve_config()
_client = APIClient(api_key=config.api_key, base_url=config.base_url)
body = {"variables": variables}
return cls(_client)._post(
f"/v1/prompts/{prompt_id}/generate",
body=body,
mode=None,
params=None,
cast_to=GenericResponse,
stream_cls=Stream[GenericResponse],
stream=False,
)
Loading

0 comments on commit 8cbf28e

Please sign in to comment.