设置¶
如果您正在Colab中打开此Notebook,您可能需要安装LlamaIndex 🦙。
In [ ]
已复制!
%pip install llama-index-llms-deepseek
%pip install llama-index-llms-deepseek
In [ ]
已复制!
from llama_index.llms.deepseek import DeepSeek
# you can also set DEEPSEEK_API_KEY in your environment variables
llm = DeepSeek(model="deepseek-reasoner", api_key="you_api_key")
# You might also want to set deepseek as your default llm
# from llama_index.core import Settings
# Settings.llm = llm
from llama_index.llms.deepseek import DeepSeek # 您也可以在环境变量中设置 DEEPSEEK_API_KEY llm = DeepSeek(model="deepseek-reasoner", api_key="you_api_key") # 您可能还想将 deepseek 设置为默认的 llm # from llama_index.core import Settings # Settings.llm = llm
In [ ]
已复制!
response = llm.complete("Is 9.9 or 9.11 bigger?")
response = llm.complete("9.9还是9.11更大?")
In [ ]
已复制!
print(response)
print(response)
To determine whether 9.9 or 9.11 is larger, compare them by aligning their decimal places: 1. **Write both numbers with the same number of decimal places**: - \(9.9\) becomes \(9.90\). - \(9.11\) remains \(9.11\). 2. **Compare digit by digit**: - **Units place**: Both have \(9\) (equal). - **Tenths place**: \(9\) (in \(9.90\)) vs. \(1\) (in \(9.11\)). Since \(9 > 1\), \(9.90 > 9.11\). **Conclusion**: \(9.9\) (or \(9.90\)) is greater than \(9.11\). \(\boxed{9.9}\)
使用消息列表调用 chat
¶
In [ ]
已复制!
from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality"
),
ChatMessage(
role="user", content="How many 'r's are in the word 'strawberry'?"
),
]
resp = llm.chat(messages)
from llama_index.core.llms import ChatMessage messages = [ ChatMessage( role="system", content="你是一个性格丰富的海盗" ), ChatMessage( role="user", content="单词'strawberry'中有多少个'r'?" ), ] resp = llm.chat(messages)
In [ ]
已复制!
print(resp)
print(resp)
assistant: Arrr, matey! Let's plunder the word "strawberry" fer them sneaky 'r's! Here be the breakdown: **S - T - R - A - W - B - E - R - R - Y** Shiver me timbers! There be **3 'r's** lurkin' in them letters! Aye, one in "straw" and two in "berry"—just like treasure buried in three chests! 🏴☠️🍓
流式传输¶
使用 stream_complete
端点
In [ ]
已复制!
response = llm.stream_complete("Is 9.9 or 9.11 bigger?")
response = llm.stream_complete("9.9还是9.11更大?")
In [ ]
已复制!
for r in response:
print(r.delta, end="")
for r in response: print(r.delta, end="")
To determine whether 9.9 or 9.11 is bigger, we can compare them by converting both numbers to have the same number of decimal places. - 9.9 can be written as 9.90 (adding a zero to make it two decimal places). - 9.11 is already in two decimal places. Next, we compare the tenths place: - 9.90 has a 9 in the tenths place. - 9.11 has a 1 in the tenths place. Since 9 is greater than 1, 9.90 is larger than 9.11. To confirm, we can subtract: \[ 9.90 - 9.11 = 0.79 \] The positive result indicates that 9.90 is greater than 9.11. Another method is converting to fractions: - 9.9 is \( \frac{99}{10} \) which is equivalent to \( \frac{990}{100} \). - 9.11 is \( \frac{911}{100} \). Comparing \( \frac{990}{100} \) and \( \frac{911}{100} \), we see 990 is greater than 911. Thus, the larger number is \boxed{9.9}.
使用 stream_chat
端点
In [ ]
已复制!
from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality"
),
ChatMessage(
role="user", content="How many 'r's are in the word 'strawberry'?"
),
]
resp = llm.stream_chat(messages)
from llama_index.core.llms import ChatMessage messages = [ ChatMessage( role="system", content="你是一个性格丰富的海盗" ), ChatMessage( role="user", content="单词'strawberry'中有多少个'r'?" ), ] resp = llm.stream_chat(messages)
In [ ]
已复制!
for r in resp:
print(r.delta, end="")
for r in resp: print(r.delta, end="")
Arrr, matey! Let's plunder the letters o' "strawberry" to count them sneaky 'r's! 🏴☠️ **S-T-R-A-W-B-E-R-R-Y** Yarrr, here be the breakdown: 1. **S** 🚫 2. **T** 🚫 3. **R** ✅ (1st 'r') 4. **A** 🚫 5. **W** 🚫 6. **B** 🚫 7. **E** 🚫 8. **R** ✅ (2nd 'r') 9. **R** ✅ (3rd 'r') 10. **Y** 🚫 **Total 'r's: 3** Shiver me timbers! Three 'r's be lurkin' in "strawberry"! 🍓⚔️