Spaces:
Build error
Build error
Download aimakerspace/openai_utils/chatmodel.py from Shipmaster1/rag-implementation: direct link, hf CLI and curl.
- Browser
- Download file 816 Bytes
-
https://huggingface.co/spaces/Shipmaster1/rag-implementation/resolve/main/aimakerspace/openai_utils/chatmodel.py
- Command line
-
hf download hf://spaces/Shipmaster1/rag-implementation/aimakerspace/openai_utils/chatmodel.py
-
curl -L -o chatmodel.py https://huggingface.co/spaces/Shipmaster1/rag-implementation/resolve/main/aimakerspace/openai_utils/chatmodel.py
816 Bytes
| import os | |
| import openai | |
| from typing import List, Dict, Union | |
| class ChatOpenAI: | |
| def __init__(self, model_name: str = "gpt-4"): | |
| self.model_name = model_name | |
| self.openai_api_key = os.getenv("OPENAI_API_KEY") | |
| if self.openai_api_key is None: | |
| raise ValueError("OPENAI_API_KEY is not set") | |
| def run(self, messages: List[Dict[str, str]], text_only: bool = True) -> Union[str, Dict]: | |
| if not isinstance(messages, list): | |
| raise ValueError("messages must be a list") | |
| openai.api_key = self.openai_api_key | |
| response = openai.ChatCompletion.create( | |
| model=self.model_name, | |
| messages=messages | |
| ) | |
| if text_only: | |
| return response.choices[0].message.content | |
| return response |