|
69 | 69 | print("Scenario 3:", response) |
70 | 70 | print("==" * 20) |
71 | 71 |
|
| 72 | +print("Scenario 3:\n") |
| 73 | +for chunk in llm.generate_stream(messages): |
| 74 | + print(chunk, end="") |
| 75 | +print("==" * 20) |
| 76 | + |
72 | 77 |
|
73 | 78 | # Scenario 4: Using LLMFactory with Huggingface Models |
74 | 79 |
|
|
91 | 96 | response = llm.generate(messages) |
92 | 97 | print("Scenario 4:", response) |
93 | 98 | print("==" * 20) |
| 99 | + |
| 100 | + |
| 101 | +# Scenario 5: Using LLMFactory with Qwen (DashScope Compatible API) |
| 102 | +# Note: |
| 103 | +# This example works for any model that supports the OpenAI-compatible Chat Completion API, |
| 104 | +# including but not limited to: |
| 105 | +# - Qwen models: qwen-plus, qwen-max-2025-01-25 |
| 106 | +# - DeepSeek models: deepseek-chat, deepseek-coder, deepseek-v3 |
| 107 | +# - Other compatible providers: MiniMax, Fireworks, Groq, OpenRouter, etc. |
| 108 | +# |
| 109 | +# Just set the correct `api_key`, `api_base`, and `model_name_or_path`. |
| 110 | + |
| 111 | +config = LLMConfigFactory.model_validate( |
| 112 | + { |
| 113 | + "backend": "qwen", |
| 114 | + "config": { |
| 115 | + "model_name_or_path": "qwen-plus", # or qwen-max-2025-01-25 |
| 116 | + "temperature": 0.7, |
| 117 | + "max_tokens": 1024, |
| 118 | + "top_p": 0.9, |
| 119 | + "top_k": 50, |
| 120 | + "api_key": "sk-xxx", |
| 121 | + "api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1", |
| 122 | + }, |
| 123 | + } |
| 124 | +) |
| 125 | +llm = LLMFactory.from_config(config) |
| 126 | +messages = [ |
| 127 | + {"role": "user", "content": "Hello, who are you"}, |
| 128 | +] |
| 129 | +response = llm.generate(messages) |
| 130 | +print("Scenario 5:", response) |
| 131 | +print("==" * 20) |
| 132 | + |
| 133 | +print("Scenario 5:\n") |
| 134 | +for chunk in llm.generate_stream(messages): |
| 135 | + print(chunk, end="") |
| 136 | +print("==" * 20) |
| 137 | + |
| 138 | +# Scenario 6: Using LLMFactory with Deepseek-chat |
| 139 | + |
| 140 | +cfg = LLMConfigFactory.model_validate( |
| 141 | + { |
| 142 | + "backend": "deepseek", |
| 143 | + "config": { |
| 144 | + "model_name_or_path": "deepseek-chat", |
| 145 | + "api_key": "sk-xxx", |
| 146 | + "api_base": "https://api.deepseek.com", |
| 147 | + "temperature": 0.6, |
| 148 | + "max_tokens": 512, |
| 149 | + "remove_think_prefix": False, |
| 150 | + }, |
| 151 | + } |
| 152 | +) |
| 153 | +llm = LLMFactory.from_config(cfg) |
| 154 | +messages = [{"role": "user", "content": "Hello, who are you"}] |
| 155 | +resp = llm.generate(messages) |
| 156 | +print("Scenario 6:", resp) |
| 157 | + |
| 158 | + |
| 159 | +# Scenario 7: Using LLMFactory with Deepseek-chat + reasoning + CoT + streaming |
| 160 | + |
| 161 | +cfg2 = LLMConfigFactory.model_validate( |
| 162 | + { |
| 163 | + "backend": "deepseek", |
| 164 | + "config": { |
| 165 | + "model_name_or_path": "deepseek-reasoner", |
| 166 | + "api_key": "sk-xxx", |
| 167 | + "api_base": "https://api.deepseek.com", |
| 168 | + "temperature": 0.2, |
| 169 | + "max_tokens": 1024, |
| 170 | + "remove_think_prefix": False, |
| 171 | + }, |
| 172 | + } |
| 173 | +) |
| 174 | +llm = LLMFactory.from_config(cfg2) |
| 175 | +messages = [ |
| 176 | + { |
| 177 | + "role": "user", |
| 178 | + "content": "Explain how to solve this problem step-by-step. Be explicit in your thinking process. Question: If a train travels from city A to city B at 60 mph and returns at 40 mph, what is its average speed for the entire trip? Let's think step by step.", |
| 179 | + }, |
| 180 | +] |
| 181 | +print("Scenario 7:\n") |
| 182 | +for chunk in llm.generate_stream(messages): |
| 183 | + print(chunk, end="") |
| 184 | +print("==" * 20) |
0 commit comments