├── .env.example
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENCE
├── README.md
└── main.py
/.env.example:
--------------------------------------------------------------------------------
1 | DEEPSEEK=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Code of Conduct for DeepSeek-API-Unofficial
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | - Using welcoming and inclusive language
12 | - Being respectful of differing viewpoints and experiences
13 | - Gracefully accepting constructive criticism
14 | - Focusing on what is best for the community
15 | - Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | - The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | - Trolling, insulting/derogatory comments, and personal or political attacks
21 | - Public or private harassment
22 | - Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | - Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [your email]. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident.
38 |
39 | ## Attribution
40 |
41 | This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html.
42 |
43 | Date: May 4, 2024
44 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 |
2 |

3 |
🌟 Contributing to DeepSeek-API-Unofficial🌟
4 |
5 |
6 |
7 |
8 |

9 |

10 |

11 |

12 |

13 |
14 |
15 | We're thrilled to have you on board and excited to see your contributions to the `DeepSeek-API-Unofficial` project. Your expertise will help shape the future of this innovative tool!
16 |
17 | ## 📜 Code of Conduct
18 |
19 | To maintain a harmonious community, we ask that you review and adhere to our [Code of Conduct](CODE_OF_CONDUCT.md).
20 |
21 | ## 🚀 Getting Started with Contributions
22 |
23 | 1. **Fork the Repository**:
24 | ```bash
25 | git fork https://github.com/SreejanPersonal/DeepSeek-API-Unofficial.git
26 | ```
27 | 2. **Clone Your Fork**:
28 | ```bash
29 | git clone https://github.com/SreejanPersonal/DeepSeek-API-Unofficial.git
30 | ```
31 | 3. **Branch Out**:
32 | ```bash
33 | git checkout -b feature/your-awesome-feature
34 | ```
35 | 4. **Commit Your Changes**:
36 | ```bash
37 | git add .
38 | git commit -m "Add a brief description of your awesome feature"
39 | ```
40 |
41 | ## 📏 Contribution Guidelines
42 |
43 | ### Code Style
44 |
45 | - Embrace the established coding conventions.
46 | - Opt for descriptive variable names and document your logic with comments.
47 |
48 | ### Testing
49 |
50 | - Craft unit tests for new features using our preferred testing framework.
51 | - Verify that all tests are green before proposing your changes.
52 |
53 | ### Documentation
54 |
55 | - Update existing documentation to mirror your modifications.
56 | - Aim for clarity and brevity in your documentation.
57 |
58 | ### Pull Requests
59 |
60 | - Initiate a pull request targeting the `main` branch.
61 | - Summarize your contributions and their impact.
62 | - Engage constructively with feedback from our maintainers.
63 |
64 | ## 🔍 Review Process
65 |
66 | Our dedicated team of maintainers will evaluate your pull request, offering insights and suggestions. Collaborate with us to refine your contribution until it shines!
67 |
68 | ## 🛠️ Additional Resources
69 |
70 | - Explore our [Youtube Video](#) for in-depth documentation.
71 | - Join the conversation on our [Telegram](#).
72 |
73 | Your participation is what drives the `DeepSeek-API-Unofficial` forward. We eagerly anticipate your innovative ideas and thank you for contributing to a brighter open-source future!
74 |
75 |
76 |
77 |
78 |

79 |

80 |

81 |

82 |

83 |
--------------------------------------------------------------------------------
/LICENCE:
--------------------------------------------------------------------------------
1 | Devs Do Code (Sree) License
2 | ==========================
3 |
4 | Version 1.0, March 2023
5 |
6 | **TERMS AND CONDITIONS**
7 |
8 | 1. **Definitions**
9 | * "Software" refers to the SreejanPersonal/Claude-3-Opus-Free-Reverse-Engineered-API software, including all source code, object code, and documentation.
10 | * "Licensor" refers to Devs Do Code (Sree), the copyright holder of the Software.
11 | * "Licensee" refers to any individual or organization using the Software.
12 | 2. **Grant of License**
13 | The Licensor hereby grants to the Licensee a non-exclusive, worldwide, royalty-free license to use, reproduce, modify, display, and distribute the Software.
14 | 3. **Conditions**
15 | The Licensee must:
16 | * Retain all copyright and proprietary notices in the Software.
17 | * Not use the Software for any illegal or malicious purposes.
18 | * Not sell or distribute the Software as a standalone product.
19 | 4. **Warranty Disclaimer**
20 | The Software is provided "as is," without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement.
21 | 5. **Limitation of Liability**
22 | In no event shall the Licensor be liable for any damages, including but not limited to incidental, consequential, or punitive damages, arising out of the use or inability to use the Software.
23 | 6. **Termination**
24 | This License will terminate automatically if the Licensee fails to comply with any of its terms and conditions.
25 | 7. **Governing Law**
26 | This License shall be governed by and construed in accordance with the laws of [Country/State].
27 |
28 | **BY USING THE SOFTWARE, YOU ACKNOWLEDGE THAT YOU HAVE READ, UNDERSTAND, AND AGREE TO BE BOUND BY THE TERMS AND CONDITIONS OF THIS LICENSE.**
29 |
30 | Copyright (c) 2023 Devs Do Code (Sree). All rights reserved.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
19 |
20 |
21 |

22 |

23 |

24 |

25 |

26 |
27 |
28 | ## Table of Contents
29 | - [Introduction](#introduction)
30 | - [Repository Status Update](#repository-status-update)
31 | - [Quick Start](#quick-start)
32 | - [Installation](#installation)
33 | - [DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model](#deepseek-v2-a-strong-economical-and-efficient-mixture-of-experts-language-model)
34 | - [1. Introduction](#1-introduction)
35 | - [2. News](#2-news)
36 | - [3. Model Downloads](#3-model-downloads)
37 | - [4. Evaluation Results](#4-evaluation-results)
38 | - [5. Model Architecture](#5-model-architecture)
39 | - [6. Chat Website](#6-chat-website)
40 | - [Contributing](#contributing)
41 | - [License](#license)
42 | - [Get in Touch](#get-in-touch)
43 |
44 | ## Crafted with ❤️ by Devs Do Code (Sree)
45 |
46 | > **Disclaimer:** This project is not officially associated with DeepSeek AI. It is an independent reverse engineering effort to explore the DeepSeek Chat Website.
47 |
48 | 🚀 **Repository Status Update:**
49 |
50 | 🛑 **Important Notice:**
51 | This repository is no longer maintained by the owner `Devs Do Code (Sree)`. Any contribution in this repository is heartily welcomed 💝💝
52 |
53 | ## 🚀 Quick Start
54 |
55 | 1. **Clone the Repository:**
56 | ```bash
57 | git clone https://github.com/SreejanPersonal/DeepSeek-API-Unofficial.git
58 | ```
59 | 2. **Access the DeepSeek Playground:**
60 | - Navigate to the [DeepSeek Playground](https://chat.deepseek.com/) and sign in with your account.
61 | - This platform allows you to interact with the available models and observe API requests.
62 |
63 | 3. **Access Developer Tools:**
64 | - Open the Developer Tools in your browser with `Ctrl + Shift + I` (Windows/Linux) or `Cmd + Option + I` (Mac).
65 | - Select the `Network` tab to monitor network activity.
66 |
67 | 4. **Initiate a New Conversation:**
68 | - Choose any available model (e.g., Coder v2 or Chat v2) on the DeepSeek Playground to start a conversation.
69 | - Enter a query in the chat interface, such as `Hi, Introduce yourself`.
70 |
71 | 5. **Locate the `completions` Request:**
72 | - In the `Network` tab, find the API request labeled `completions`.
73 | - Click on this request to inspect its details.
74 |
75 | 6. **Obtain the JWT Token:**
76 | - In the `Request Headers` section, locate the `Authorization` entry.
77 | - Copy the `JWT Token` value (it appears as a long string without the `Bearer` prefix). This token serves as your API key and must be kept confidential.
78 | - Example format: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzc29faWQiOiI3OTg3ZTZmYS1kZDUzLTRlMzQtYjkxNC1lNWUzZWVlM2IwYjMiLCJpc19vYXV0aCI6MSwib2F1dGhfcHJvdmlkZXIiOiJ......`
79 |
80 | ## 🛠️ Installation
81 |
82 | After cloning the repository and obtaining your `JWT Token`, follow these steps to set up and run the project:
83 |
84 | 1. **Navigate to the Project Directory:**
85 | ```bash
86 | cd DeepSeek-API-Unofficial
87 | ```
88 |
89 | 2. **Create a `.env` File:**
90 | - Inside the project directory, create a `.env` file.
91 | - Add your `JWT Token` to this file. You can use the provided [.env.example](.env.example) file as a reference.
92 | ```bash
93 | cp .env.example .env
94 | ```
95 | - Open the `.env` file and insert your token:
96 | ```
97 | DEEPSEEK=your_jwt_token_here
98 | ```
99 |
100 | 3. **Install Required Dependencies:**
101 | - Ensure you have `pip` installed and run:
102 | ```bash
103 | pip install -r requirements.txt
104 | ```
105 |
106 | 4. **Run the Application:**
107 | - Execute the main script to start interacting with the DeepSeek API:
108 | ```bash
109 | python main.py
110 | ```
111 |
112 | By following these steps, you will set up the environment and be able to interact with the DeepSeek models using the unofficial API.
113 |
114 | # DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model
115 |
116 | ## 1. Introduction
117 | Today, we’re introducing DeepSeek-V2, a strong Mixture-of-Experts (MoE) language model characterized by economical training and efficient inference. It comprises 236B total parameters, of which 21B are activated for each token. Compared with DeepSeek 67B, DeepSeek-V2 achieves stronger performance, and meanwhile saves 42.5% of training costs, reduces the KV cache by 93.3%, and boosts the maximum generation throughput to 5.76 times.
118 |
119 |
120 |
121 |

122 |

123 |
124 |
125 |
126 | We pretrained DeepSeek-V2 on a diverse and high-quality corpus comprising 8.1 trillion tokens. This comprehensive pretraining was followed by a process of Supervised Fine-Tuning (SFT) and Reinforcement Learning (RL) to fully unleash the model's capabilities. The evaluation results validate the effectiveness of our approach as DeepSeek-V2 achieves remarkable performance on both standard benchmarks and open-ended generation evaluation.
127 |
128 | ## 2. News
129 |
130 | - 2024.05.16: We released the DeepSeek-V2-Lite.
131 | - 2024.05.06: We released the DeepSeek-V2.
132 |
133 | ## 3. Model Downloads
134 |
135 |
136 |
137 | | **Model** | **#Total Params** | **#Activated Params** | **Context Length** | **Download** |
138 | | :------------: | :------------: | :------------: | :------------: | :------------: |
139 | | DeepSeek-V2-Lite | 16B | 2.4B | 32k | [🤗 HuggingFace](https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite) |
140 | | DeepSeek-V2-Lite-Chat (SFT) | 16B | 2.4B | 32k | [🤗 HuggingFace](https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite-Chat) |
141 | | DeepSeek-V2 | 236B | 21B | 128k | [🤗 HuggingFace](https://huggingface.co/deepseek-ai/DeepSeek-V2) |
142 | | DeepSeek-V2-Chat (RL) | 236B | 21B | 128k | [🤗 HuggingFace](https://huggingface.co/deepseek-ai/DeepSeek-V2-Chat) |
143 |
144 |
145 |
146 | Due to the constraints of HuggingFace, the open-source code currently experiences slower performance than our internal codebase when running on GPUs with Huggingface. To facilitate the efficient execution of our model, we offer a dedicated vllm solution that optimizes performance for running our model effectively.
147 |
148 | ## 4. Evaluation Results
149 | ### Base Model
150 | #### Standard Benchmark (Models larger than 67B)
151 |
152 |
153 |
154 | | **Benchmark** | **Domain** | **LLaMA3 70B** | **Mixtral 8x22B** | **DeepSeek-V1 (Dense-67B)** | **DeepSeek-V2 (MoE-236B)** |
155 | |:-----------:|:--------:|:------------:|:---------------:|:-------------------------:|:------------------------:|
156 | | **MMLU** | English | 78.9 | 77.6 | 71.3 | 78.5 |
157 | | **BBH** | English | 81.0 | 78.9 | 68.7 | 78.9 |
158 | | **C-Eval** | Chinese | 67.5 | 58.6 | 66.1 | 81.7 |
159 | | **CMMLU** | Chinese | 69.3 | 60.0 | 70.8 | 84.0 |
160 | | **HumanEval** | Code | 48.2 | 53.1 | 45.1 | 48.8 |
161 | | **MBPP** | Code | 68.6 | 64.2 | 57.4 | 66.6 |
162 | | **GSM8K** | Math | 83.0 | 80.3 | 63.4 | 79.2 |
163 | | **Math** | Math | 42.2 | 42.5 | 18.7 | 43.6 |
164 |
165 |
166 |
167 | #### Standard Benchmark (Models smaller than 16B)
168 |
169 |
170 | | **Benchmark** | **Domain** | **DeepSeek 7B (Dense)** | **DeepSeekMoE 16B** | **DeepSeek-V2-Lite (MoE-16B)** |
171 | |:-------------:|:----------:|:--------------:|:-----------------:|:--------------------------:|
172 | | **Architecture** | - | MHA+Dense | MHA+MoE | MLA+MoE |
173 | | **MMLU** | English | 48.2 | 45.0 | 58.3 |
174 | | **BBH** | English | 39.5 | 38.9 | 44.1 |
175 | | **C-Eval** | Chinese | 45.0 | 40.6 | 60.3 |
176 | | **CMMLU** | Chinese | 47.2 | 42.5 | 64.3 |
177 | | **HumanEval** | Code | 26.2 | 26.8 | 29.9 |
178 | | **MBPP** | Code | 39.0 | 39.2 | 43.2 |
179 | | **GSM8K** | Math | 17.4 | 18.8 | 41.1 |
180 | | **Math** | Math | 3.3 | 4.3 | 17.1 |
181 |
182 |
183 | For more evaluation details, such as few-shot settings and prompts, please check our paper.
184 |
185 | #### Context Window
186 |
187 |
188 |
189 |
190 | Evaluation results on the ``Needle In A Haystack`` (NIAH) tests. DeepSeek-V2 performs well across all context window lengths up to **128K**.
191 |
192 | ### Chat Model
193 | #### Standard Benchmark (Models larger than 67B)
194 |
195 |
196 | | Benchmark | Domain | QWen1.5 72B Chat | Mixtral 8x22B | LLaMA3 70B Instruct | DeepSeek-V1 Chat (SFT) | DeepSeek-V2 Chat (SFT) | DeepSeek-V2 Chat (RL) |
197 | |:-----------:|:----------------:|:------------------:|:---------------:|:---------------------:|:-------------:|:-----------------------:|:----------------------:|
198 | | **MMLU** | English | 76.2 | 77.8 | 80.3 | 71.1 | 78.4 | 77.8 |
199 | | **BBH** | English | 65.9 | 78.4 | 80.1 | 71.7 | 81.3 | 79.7 |
200 | | **C-Eval** | Chinese | 82.2 | 60.0 | 67.9 | 65.2 | 80.9 | 78.0 |
201 | | **CMMLU** | Chinese | 82.9 | 61.0 | 70.7 | 67.8 | 82.4 | 81.6 |
202 | | **HumanEval** | Code | 68.9 | 75.0 | 76.2 | 73.8 | 76.8 | 81.1 |
203 | | **MBPP** | Code | 52.2 | 64.4 | 69.8 | 61.4 | 70.4 | 72.0 |
204 | | **LiveCodeBench (0901-0401)** | Code | 18.8 | 25.0 | 30.5 | 18.3 | 28.7 | 32.5 |
205 | | **GSM8K** | Math | 81.9 | 87.9 | 93.2 | 84.1 | 90.8 | 92.2 |
206 | | **Math** | Math | 40.6 | 49.8 | 48.5 | 32.6 | 52.7 | 53.9 |
207 |
208 |
209 |
210 | #### Standard Benchmark (Models smaller than 16B)
211 |
212 |
213 |
214 | | Benchmark | Domain | DeepSeek 7B Chat (SFT) | DeepSeekMoE 16B Chat (SFT) | DeepSeek-V2-Lite 16B Chat (SFT) |
215 | |:-----------:|:----------------:|:------------------:|:---------------:|:---------------------:|
216 | | **MMLU** | English | 49.7 | 47.2 | 55.7 |
217 | | **BBH** | English | 43.1 | 42.2 | 48.1 |
218 | | **C-Eval** | Chinese | 44.7 | 40.0 | 60.1 |
219 | | **CMMLU** | Chinese | 51.2 | 49.3 | 62.5 |
220 | | **HumanEval** | Code | 45.1 | 45.7 | 57.3 |
221 | | **MBPP** | Code | 39.0 | 46.2 | 45.8 |
222 | | **GSM8K** | Math | 62.6 | 62.2 | 72.0 |
223 | | **Math** | Math | 14.7 | 15.2 | 27.9 |
224 |
225 |
226 |
227 | #### English Open Ended Generation Evaluation
228 | We evaluate our model on AlpacaEval 2.0 and MTBench, showing the competitive performance of DeepSeek-V2-Chat-RL on English conversation generation.
229 |
230 |
231 |
232 |
233 | #### Chinese Open Ended Generation Evaluation
234 | **Alignbench** (https://arxiv.org/abs/2311.18743)
235 |
236 |
237 | | **模型** | **开源/闭源** | **总分** | **中文推理** | **中文语言** |
238 | | :---: | :---: | :---: | :---: | :---: |
239 | | gpt-4-1106-preview | 闭源 | 8.01 | 7.73 | 8.29 |
240 | | DeepSeek-V2 Chat (RL) | 开源 | 7.91 | 7.45 | 8.36 |
241 | | erniebot-4.0-202404 (文心一言) | 闭源 | 7.89 | 7.61 | 8.17 |
242 | | DeepSeek-V2 Chat (SFT) | 开源 | 7.74 | 7.30 | 8.17 |
243 | | gpt-4-0613 | 闭源 | 7.53 | 7.47 | 7.59 |
244 | | erniebot-4.0-202312 (文心一言) | 闭源 | 7.36 | 6.84 | 7.88 |
245 | | moonshot-v1-32k-202404 (月之暗面) | 闭源 | 7.22 | 6.42 | 8.02 |
246 | | Qwen1.5-72B-Chat (通义千问) | 开源 | 7.19 | 6.45 | 7.93 |
247 | | DeepSeek-67B-Chat | 开源 | 6.43 | 5.75 | 7.11 |
248 | | Yi-34B-Chat (零一万物) | 开源 | 6.12 | 4.86 | 7.38 |
249 | | gpt-3.5-turbo-0613 | 闭源 | 6.08 | 5.35 | 6.71 |
250 | | DeepSeek-V2-Lite 16B Chat | 开源 | 6.01 | 4.71 | 7.32 |
251 |
252 |
253 |
254 | #### Coding Benchmarks
255 | We evaluate our model on LiveCodeBench (0901-0401), a benchmark designed for live coding challenges. As illustrated, DeepSeek-V2 demonstrates considerable proficiency in LiveCodeBench, achieving a Pass@1 score that surpasses several other sophisticated models. This performance highlights the model's effectiveness in tackling live coding tasks.
256 |
257 |
258 |
259 |
260 |
261 | ## 5. Model Architecture
262 | DeepSeek-V2 adopts innovative architectures to guarantee economical training and efficient inference:
263 | - For attention, we design MLA (Multi-head Latent Attention), which utilizes low-rank key-value union compression to eliminate the bottleneck of inference-time key-value cache, thus supporting efficient inference.
264 | - For Feed-Forward Networks (FFNs), we adopt DeepSeekMoE architecture, a high-performance MoE architecture that enables training stronger models at lower costs.
265 |
266 |
267 |
268 |
269 |
270 | ## 6. Chat Website
271 | You can chat with the DeepSeek-V2 on DeepSeek's official website: [chat.deepseek.com](https://chat.deepseek.com/sign_in)
272 |
273 | ### 🤝 Contributing
274 |
275 | Your contributions are welcome! Please refer to our [CONTRIBUTING.md](CONTRIBUTING.md) for contribution guidelines.
276 |
277 | ### 📜 License
278 |
279 | This project is licensed under the [MIT License](LICENSE). Full license text is available in the [LICENSE](LICENSE) file.
280 |
281 | ### 📬 Get in Touch
282 |
283 | For inquiries or assistance, please open an issue or reach out through our social channels:
284 |
285 |
286 |
287 |

288 |

289 |

290 |

291 |

292 |
293 |
294 | We appreciate your interest in `DeepSeek-API-Unofficial`
295 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | import requests
2 | import json
3 | import re
4 | import os
5 | from typing import Optional
6 | from dotenv import load_dotenv; load_dotenv()
7 |
8 | class DeepSeekAPI:
9 | """
10 | A class to interact with the DeepSeek API for initiating chat sessions.
11 | """
12 |
13 | def __init__(self, api_token: Optional[str] = os.environ.get("DEEPSEEK")):
14 | """
15 | Initializes the DeepSeekAPI with necessary authorization headers.
16 |
17 | Args:
18 | api_token (str): The Bearer token for API authorization.
19 | """
20 | self.auth_headers = {
21 | 'Authorization': f'Bearer {api_token}'
22 | }
23 | self.api_base_url = 'https://chat.deepseek.com/api/v0/chat'
24 | self.api_session = requests.Session()
25 | self.api_session.headers.update(self.auth_headers)
26 |
27 | def clear_chat(self) -> None:
28 | """
29 | Clears the chat context by making a POST request to the clear_context endpoint.
30 | """
31 | clear_payload = {"model_class": "deepseek_chat", "append_welcome_message": False}
32 | clear_response = self.api_session.post(f'{self.api_base_url}/clear_context', json=clear_payload)
33 | clear_response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
34 |
35 | def generate(self, user_message: str, response_temperature: float = 1.0, model_type: Optional[str] = "deepseek_chat", verbose: bool = False, system_prompt: Optional[str] = "Be Short & Concise") -> str:
36 | """
37 | Generates a response from the DeepSeek API based on the provided message.
38 |
39 | Args:
40 | user_message (str): The message to send to the chat API.
41 | response_temperature (float, optional): The creativity level of the response. Defaults to 1.0.
42 | model_type (str, optional): The model class to be used for the chat session.
43 | verbose (bool, optional): Whether to print the response content. Defaults to False.
44 | system_prompt (str, optional): The system prompt to be used. Defaults to "Be Short & Concise".
45 |
46 | Returns:
47 | str: The concatenated response content received from the API.
48 |
49 | Available models:
50 | - deepseek_chat
51 | - deepseek_code
52 | """
53 | request_payload = {
54 | "message": f"[Instructions: {system_prompt}]\n\nUser Query:{user_message}",
55 | "stream": True,
56 | "model_preference": None,
57 | "model_class": model_type,
58 | "temperature": response_temperature
59 | }
60 | api_response = self.api_session.post(f'{self.api_base_url}/completions', json=request_payload, stream=True)
61 | api_response.raise_for_status()
62 |
63 | combined_response = ""
64 | for response_line in api_response.iter_lines(decode_unicode=True, chunk_size=1):
65 | if response_line:
66 | cleaned_line = re.sub("data:", "", response_line)
67 | response_json = json.loads(cleaned_line)
68 | response_content = response_json['choices'][0]['delta']['content']
69 | if response_content and not re.match(r'^\s{5,}$', response_content):
70 | if verbose: print(response_content, end="", flush=True)
71 | combined_response += response_content
72 |
73 | return combined_response
74 |
75 | # Example usage
76 | if __name__ == "__main__":
77 |
78 | api = DeepSeekAPI()
79 | while True:
80 | print("\nYou: ", end="", flush=True)
81 | user_query = input()
82 |
83 | if user_query == "/bye":
84 | api.clear_chat()
85 | break
86 |
87 | print("AI: ", end="", flush=True)
88 | api_response_content = api.generate(user_message=user_query, model_type='deepseek_chat', verbose=True)
89 | # print("\n\n" + api_response_content)
--------------------------------------------------------------------------------