Spaces:
Sleeping
Sleeping
| from openai import OpenAI # type: ignore | |
| import os | |
| def call_openai( | |
| user_prompt, | |
| chat_history: list[tuple[str, str]], | |
| system_prompt, | |
| max_tokens, | |
| temperature, | |
| top_p, | |
| file_upload=None, | |
| image_upload=None | |
| ): | |
| if file_upload == None: | |
| try: | |
| pass | |
| except: | |
| pass | |
| if image_upload == None: | |
| try: | |
| pass | |
| except: | |
| pass | |
| #read system message | |
| messages = [{"role": "system", "content": system_prompt}] | |
| #read history | |
| for user_chat, assistant_chat in chat_history: | |
| if user_chat: | |
| messages.append({"role": "user", "content": user_chat}) | |
| if assistant_chat: | |
| messages.append({"role": "assistant", "content": assistant_chat}) | |
| #read output | |
| messages.append({"role": "user", "content": user_prompt}) | |
| print("## Messages: \n", messages) #debug output | |
| #create output | |
| response = OpenAI().responses.create( | |
| model="gpt-4.1-nano", | |
| input=messages, | |
| temperature=temperature, | |
| top_p=top_p, | |
| max_output_tokens=max_tokens | |
| ) | |
| #read output | |
| response = response.output_text | |
| print("## Response: ", response) #debug output | |
| print("\n") | |
| yield response #chat reply | |
| deepseek = OpenAI(api_key = os.getenv("DEEPSEEK_API_KEY"), base_url="https://api.deepseek.com") | |
| def call_deepseek( | |
| user_prompt: str, | |
| chat_history: list[tuple[str, str]], | |
| system_prompt: str, | |
| max_tokens: int, | |
| temperature: float, | |
| top_p: float, | |
| file_upload=None, | |
| image_upload=None | |
| ): | |
| """ | |
| Gọi DeepSeek Chat qua OpenAI-compatible API (không stream). | |
| - file_upload và image_upload tùy chọn (None để bỏ qua xử lý). | |
| Trả về: | |
| - reply (str): nội dung model sinh ra. | |
| """ | |
| # 1. Xử lý tùy chọn file (nếu có) | |
| if file_upload == None: | |
| try: | |
| pass | |
| except: | |
| pass | |
| if image_upload == None: | |
| try: | |
| pass | |
| except: | |
| pass | |
| # 3. Xây dựng messages lịch sử chat | |
| messages = [{"role": "system", "content": system_prompt}] | |
| for user_msg, ai_msg in chat_history: | |
| if user_msg: | |
| messages.append({"role": "user", "content": user_msg}) | |
| if ai_msg: | |
| messages.append({"role": "assistant", "content": ai_msg}) | |
| # Thêm prompt hiện tại của user | |
| messages.append({"role": "user", "content": user_prompt}) | |
| # 4. Gọi API DeepSeek Chat (OpenAI-compatible) | |
| response = OpenAI(api_key = os.getenv("DEEPSEEK_API_KEY"), base_url="https://api.deepseek.com").chat.completions.create( | |
| model="deepseek-chat", # hoặc model bạn cấu hình | |
| messages=messages, | |
| temperature=temperature, | |
| top_p=top_p, | |
| max_tokens=max_tokens | |
| ) | |
| # 5. Trích xuất kết quả trả về | |
| reply = response.choices[0].message.content | |
| return reply | |
| # 1. Hàm gọi DeepSeek + build/append history | |
| def call_deepseek_new( | |
| user_prompt, | |
| chat_history, # sẽ là [{"role":"user"/"assistant","content":...}, …] | |
| # system_prompt: str, | |
| # max_tokens: int, | |
| # temperature: float, | |
| # top_p: float, | |
| # file_upload=None, | |
| # image_upload=None | |
| ): | |
| # Khởi tạo history nếu None | |
| history = chat_history or [] | |
| # Append system prompt (chỉ ở lần đầu nếu bạn muốn) | |
| # if not any(m["role"]=="system" for m in history): | |
| # history.insert(0, {"role": "system", "content": system_prompt}) | |
| # Append message mới của user | |
| history.append({"role": "user", "content": user_prompt}) | |
| # Gọi API DeepSeek Chat (OpenAI-compatible, không stream) | |
| response = deepseek.chat.completions.create( | |
| model = "deepseek-chat", # hoặc model bạn đã config | |
| messages = history, | |
| # temperature= temperature, | |
| # top_p = top_p, | |
| # max_tokens = max_tokens | |
| ) | |
| # Lấy nội dung assistant trả về | |
| reply = response.choices[0].message.content | |
| # Append vào history | |
| history.append({"role": "assistant", "content": reply}) | |
| # Trả về 2 outputs: toàn bộ history và đúng reply để render Markdown | |
| return history, reply | |
| """ | |
| Không có billing nên không xài được replicate | |
| """ | |
| # import replicate | |
| # def deepseek_api_replicate( | |
| # user_prompt, | |
| # history: list[tuple[str, str]], | |
| # system_prompt, | |
| # max_new_tokens, | |
| # temperature, | |
| # top_p): | |
| # """ | |
| # Gọi DeepSeek Math trên Replicate và trả ngay kết quả. | |
| # Trả về: | |
| # str hoặc [bytes]: output model sinh ra | |
| # """ | |
| # # 1. Khởi tạo client và xác thực | |
| # # token = os.getenv("REPLICATE_API_TOKEN") | |
| # # if not token: | |
| # # raise RuntimeError("Missing REPLICATE_API_TOKEN") # bảo mật bằng biến môi trường | |
| # client = replicate.Client(api_token="REPLICATE_API_TOKEN") | |
| # # 2. Gọi model | |
| # output = client.run( | |
| # "deepseek-ai/deepseek-math-7b-base:61f572dae0985541cdaeb4a114fd5d2d16cb40dac3894da10558992fc60547c7", | |
| # input={ | |
| # "system_prompt": system_prompt, | |
| # "user_prompt": user_prompt, | |
| # "max_new_tokens": max_new_tokens, | |
| # "temperature": temperature, | |
| # "top_p": top_p | |
| # } | |
| # ) | |
| # # 3. Trả kết quả | |
| # return output |