Spaces:
Sleeping
Sleeping
Download openai_client.py from Corin1998/ESG_IR_RAGbot: direct link, hf CLI and curl.
- Browser
- Download file 801 Bytes
-
https://huggingface.co/spaces/Corin1998/ESG_IR_RAGbot/resolve/4da17068a3c4b4ecb9574f6f46e964baa42b33fa/openai_client.py
- Command line
-
hf download hf://spaces/Corin1998/ESG_IR_RAGbot@4da17068a3c4b4ecb9574f6f46e964baa42b33fa/openai_client.py
-
curl -L -o openai_client.py https://huggingface.co/spaces/Corin1998/ESG_IR_RAGbot/resolve/4da17068a3c4b4ecb9574f6f46e964baa42b33fa/openai_client.py
801 Bytes
| from __future__ import annotations | |
| from typing import List, Dict | |
| from openai import OpenAI | |
| _client = None | |
| def client() -> OpenAI: | |
| global _client | |
| if _client is None: | |
| # OPENAI_API_KEY / OPENAI_BASE_URL は環境変数から取得 | |
| _client = OpenAI() | |
| return _client | |
| # Embeddings | |
| def embed_texts(texts: List[str], model: str) -> List[List[float]]: | |
| resp = client().embeddings.create(model=model, input=texts) | |
| return [d.embedding for d in resp.data] | |
| # Responses API | |
| def chat(messages: List[Dict], model: str, max_output_tokens: int = 700, temperature: float = 0.2) -> str: | |
| resp = client().responses.create( | |
| model=model, | |
| input=messages, | |
| max_output_tokens=max_output_tokens, | |
| temperature=temperature, | |
| ) | |
| return resp.output_text | |