|
| 1 | +from typing import Dict, List, Literal, Optional, Type, Union |
| 2 | + |
| 3 | +import httpx |
| 4 | +from openai import BaseModel |
| 5 | + |
| 6 | +from edenai_apis.features import ProviderInterface, TextInterface |
| 7 | +from edenai_apis.features.multimodal.chat.chat_dataclass import ( |
| 8 | + ChatDataClass as ChatMultimodalDataClass, |
| 9 | +) |
| 10 | +from edenai_apis.features.multimodal.chat.chat_dataclass import ( |
| 11 | + ChatMessageDataClass as ChatMultimodalMessageDataClass, |
| 12 | +) |
| 13 | +from edenai_apis.features.multimodal.chat.chat_dataclass import ( |
| 14 | + StreamChat as StreamChatMultimodal, |
| 15 | +) |
| 16 | +from edenai_apis.features.text.chat.chat_dataclass import ChatDataClass, StreamChat |
| 17 | +from edenai_apis.llmengine.llm_engine import LLMEngine |
| 18 | +from edenai_apis.loaders.data_loader import ProviderDataEnum |
| 19 | +from edenai_apis.loaders.loaders import load_provider |
| 20 | +from edenai_apis.utils.types import ResponseType |
| 21 | +from edenai_apis.features.llm.llm_interface import LlmInterface |
| 22 | +from edenai_apis.features.llm.chat.chat_dataclass import ChatDataClass |
| 23 | + |
| 24 | + |
| 25 | +class GroqApi(ProviderInterface, TextInterface, LlmInterface): |
| 26 | + provider_name = "groq" |
| 27 | + |
| 28 | + def __init__(self, api_keys: Dict = {}) -> None: |
| 29 | + self.api_settings = load_provider( |
| 30 | + ProviderDataEnum.KEY, self.provider_name, api_keys=api_keys |
| 31 | + ) |
| 32 | + self.llm_client = LLMEngine( |
| 33 | + provider_name=self.provider_name, |
| 34 | + provider_config={ |
| 35 | + "api_key": self.api_settings.get("api_key"), |
| 36 | + }, |
| 37 | + ) |
| 38 | + |
| 39 | + def text__chat( |
| 40 | + self, |
| 41 | + text: str, |
| 42 | + chatbot_global_action: Optional[str] = None, |
| 43 | + previous_history: Optional[List[Dict[str, str]]] = None, |
| 44 | + temperature: float = 0.0, |
| 45 | + max_tokens: int = 25, |
| 46 | + model: Optional[str] = None, |
| 47 | + stream: bool = False, |
| 48 | + available_tools: Optional[List[dict]] = None, |
| 49 | + tool_choice: Literal["auto", "required", "none"] = "auto", |
| 50 | + tool_results: Optional[List[dict]] = None, |
| 51 | + **kwargs, |
| 52 | + ) -> ResponseType[Union[ChatDataClass, StreamChat]]: |
| 53 | + response = self.llm_client.chat( |
| 54 | + text=text, |
| 55 | + previous_history=previous_history, |
| 56 | + chatbot_global_action=chatbot_global_action, |
| 57 | + temperature=temperature, |
| 58 | + max_tokens=max_tokens, |
| 59 | + model=model, |
| 60 | + stream=stream, |
| 61 | + available_tools=available_tools, |
| 62 | + tool_choice=tool_choice, |
| 63 | + tool_results=tool_results, |
| 64 | + **kwargs, |
| 65 | + ) |
| 66 | + return response |
| 67 | + |
| 68 | + def multimodal__chat( |
| 69 | + self, |
| 70 | + messages: List[ChatMultimodalMessageDataClass], |
| 71 | + chatbot_global_action: Optional[str], |
| 72 | + temperature: float = 0, |
| 73 | + max_tokens: int = 25, |
| 74 | + model: Optional[str] = None, |
| 75 | + stop_sequences: Optional[List[str]] = None, |
| 76 | + top_k: Optional[int] = None, |
| 77 | + top_p: Optional[int] = None, |
| 78 | + stream: bool = False, |
| 79 | + provider_params: Optional[dict] = None, |
| 80 | + response_format=None, |
| 81 | + **kwargs, |
| 82 | + ) -> ResponseType[Union[ChatMultimodalDataClass, StreamChatMultimodal]]: |
| 83 | + response = self.llm_client.multimodal_chat( |
| 84 | + messages=messages, |
| 85 | + chatbot_global_action=chatbot_global_action, |
| 86 | + temperature=temperature, |
| 87 | + max_tokens=max_tokens, |
| 88 | + model=model, |
| 89 | + stop_sequences=stop_sequences, |
| 90 | + top_k=top_k, |
| 91 | + top_p=top_p, |
| 92 | + stream=stream, |
| 93 | + response_format=response_format, |
| 94 | + **kwargs, |
| 95 | + ) |
| 96 | + return response |
| 97 | + |
| 98 | + def llm__chat( |
| 99 | + self, |
| 100 | + messages: List = [], |
| 101 | + model: Optional[str] = None, |
| 102 | + timeout: Optional[Union[float, str, httpx.Timeout]] = None, |
| 103 | + temperature: Optional[float] = None, |
| 104 | + top_p: Optional[float] = None, |
| 105 | + n: Optional[int] = None, |
| 106 | + stream: Optional[bool] = None, |
| 107 | + stream_options: Optional[dict] = None, |
| 108 | + stop: Optional[str] = None, |
| 109 | + stop_sequences: Optional[any] = None, |
| 110 | + max_tokens: Optional[int] = None, |
| 111 | + presence_penalty: Optional[float] = None, |
| 112 | + frequency_penalty: Optional[float] = None, |
| 113 | + logit_bias: Optional[dict] = None, |
| 114 | + # openai v1.0+ new params |
| 115 | + response_format: Optional[ |
| 116 | + Union[dict, Type[BaseModel]] |
| 117 | + ] = None, # Structured outputs |
| 118 | + seed: Optional[int] = None, |
| 119 | + tools: Optional[List] = None, |
| 120 | + tool_choice: Optional[Union[str, dict]] = None, |
| 121 | + logprobs: Optional[bool] = None, |
| 122 | + top_logprobs: Optional[int] = None, |
| 123 | + parallel_tool_calls: Optional[bool] = None, |
| 124 | + deployment_id=None, |
| 125 | + extra_headers: Optional[dict] = None, |
| 126 | + # soon to be deprecated params by OpenAI -> This should be replaced by tools |
| 127 | + functions: Optional[List] = None, |
| 128 | + function_call: Optional[str] = None, |
| 129 | + base_url: Optional[str] = None, |
| 130 | + api_version: Optional[str] = None, |
| 131 | + api_key: Optional[str] = None, |
| 132 | + model_list: Optional[list] = None, # pass in a list of api_base,keys, etc. |
| 133 | + drop_invalid_params: bool = True, # If true, all the invalid parameters will be ignored (dropped) before sending to the model |
| 134 | + user: str | None = None, |
| 135 | + # Optional parameters |
| 136 | + **kwargs, |
| 137 | + ) -> ChatDataClass: |
| 138 | + response = self.llm_client.completion( |
| 139 | + messages=messages, |
| 140 | + model=model, |
| 141 | + timeout=timeout, |
| 142 | + temperature=temperature, |
| 143 | + top_p=top_p, |
| 144 | + n=n, |
| 145 | + stream=stream, |
| 146 | + stream_options=stream_options, |
| 147 | + stop=stop, |
| 148 | + stop_sequences=stop_sequences, |
| 149 | + max_tokens=max_tokens, |
| 150 | + presence_penalty=presence_penalty, |
| 151 | + frequency_penalty=frequency_penalty, |
| 152 | + logit_bias=logit_bias, |
| 153 | + response_format=response_format, |
| 154 | + seed=seed, |
| 155 | + tools=tools, |
| 156 | + tool_choice=tool_choice, |
| 157 | + logprobs=logprobs, |
| 158 | + top_logprobs=top_logprobs, |
| 159 | + parallel_tool_calls=parallel_tool_calls, |
| 160 | + deployment_id=deployment_id, |
| 161 | + extra_headers=extra_headers, |
| 162 | + functions=functions, |
| 163 | + function_call=function_call, |
| 164 | + base_url=base_url, |
| 165 | + api_version=api_version, |
| 166 | + api_key=api_key, |
| 167 | + model_list=model_list, |
| 168 | + drop_invalid_params=drop_invalid_params, |
| 169 | + user=user, |
| 170 | + **kwargs, |
| 171 | + ) |
| 172 | + return response |
0 commit comments