Skip to content

Commit

Permalink
Enhance SDK support and refactor wrapper classes
Browse files Browse the repository at this point in the history
- Add support for HuggingFace SDK
- Implement StreamWrapper for LLMDK
- Refactor GenerateWrapper to use unified prompt_or_messages parameter
- Update README with new supported SDKs and example usage
- Improve type hinting and code formatting
  • Loading branch information
brunneis committed Oct 8, 2024
1 parent 6e9d120 commit 8ff85c0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ from openai import OpenAI
from os import environ as env

client = OpenAI(
api_key=env.get('OPENAI_API_KEY')
api_key=env.get('OPENAI_API_KEY'),
)

# Wrap your client with Nootropic
client = Nootropic(
client,
# prefix='prompt prefix for every user message',
# postfix='prompt postfix for every user message',
system='system prompt for the model',
# prefix='Prefix for every user message.',
# postfix='Postfix for every user message.',
# system='System prompt.',
# disable_cache=False,
)

```

# Supported Python SDKs
# Supported SDKs
- LLMDK: [llmdk](https://github.com/EveripediaNetwork/llmdk)
- OpenAI: [openai](https://github.com/openai/openai-python)
- Anthropic: [anthropic](https://github.com/anthropics/anthropic-sdk-python)
- Groq: [groq](https://github.com/groq/groq-python)
- HuggingFace: [huggingface](https://github.com/huggingface/huggingface_hub)
- Ollama: [ollama](https://github.com/ollama/ollama-python)
- OpenAI: [openai](https://github.com/openai/openai-python)
56 changes: 46 additions & 10 deletions nootropic/nootropic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Union
import uuid
import copy

Expand Down Expand Up @@ -81,18 +81,21 @@ def __getattr__(self, name: str) -> Any:
class GenerateWrapper(BaseWrapper):
def __call__(
self,
prompt: Optional[str] = None,
system_prompt: Optional[str] = None,
messages: Optional[List[Dict[str, str]]] = None,
prompt_or_messages: Union[str, List[Dict[str, str]]],
system: Optional[str] = None,
**kwargs: Any,
) -> str:
if messages:
kwargs['messages'] = self._modify_messages(messages)
elif prompt:
kwargs['prompt'] = self._modify_content(prompt)
if isinstance(prompt_or_messages, list):
kwargs['prompt_or_messages'] = self._modify_messages(
prompt_or_messages,
)
else:
kwargs['prompt_or_messages'] = self._modify_content(
prompt_or_messages,
)

if system_prompt or self._system:
kwargs['system_prompt'] = system_prompt or self._system
if system or self._system:
kwargs['system'] = system or self._system

return self._client_attr(**kwargs)

Expand All @@ -102,6 +105,34 @@ def __getattr__(self, name: str) -> Any:
)


# llmdk
class StreamWrapper(BaseWrapper):
def __call__(
self,
prompt_or_messages: Union[str, List[Dict[str, str]]],
system: Optional[str] = None,
**kwargs: Any,
) -> Any:
if isinstance(prompt_or_messages, list):
kwargs['prompt_or_messages'] = self._modify_messages(
prompt_or_messages,
)
else:
kwargs['prompt_or_messages'] = self._modify_content(
prompt_or_messages,
)

if system or self._system:
kwargs['system'] = system or self._system

return self._client_attr(**kwargs)

def __getattr__(self, name: str) -> Any:
raise NotImplementedError(
'Method {0} is not implemented in StreamWrapper'.format(name),
)


# OpenAI, HuggingFace, Groq, Ollama
class ChatWrapper(BaseWrapper):
def __call__(self, **kwargs: Any) -> Any:
Expand Down Expand Up @@ -201,6 +232,11 @@ def _create_wrapper(self, attr_name: str, wrapper_class: type) -> Any:
def generate(self) -> GenerateWrapper:
return self._create_wrapper('generate', GenerateWrapper)

# LLMDK
@property
def stream(self) -> StreamWrapper:
return self._create_wrapper('stream', StreamWrapper)

# OpenAI, HuggingFace, Groq, Ollama
@property
def chat(self) -> ChatWrapper:
Expand Down

0 comments on commit 8ff85c0

Please sign in to comment.