llama-index llms dashscope integration
Project description
LlamaIndex Llms Integration: Dashscope
Installation
-
Install the required Python package:
pip install llama-index-llms-dashscope
-
Set the DashScope API key as an environment variable:
export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY
Alternatively, you can set it in your Python script:
import os os.environ["DASHSCOPE_API_KEY"] = "YOUR_DASHSCOPE_API_KEY"
Usage
Basic Recipe Generation
To generate a basic vanilla cake recipe:
from llama_index.llms.dashscope import DashScope, DashScopeGenerationModels
# Initialize DashScope object
dashscope_llm = DashScope(model_name=DashScopeGenerationModels.QWEN_MAX)
# Generate a vanilla cake recipe
resp = dashscope_llm.complete("How to make cake?")
print(resp)
Streaming Recipe Responses
For real-time streamed responses:
responses = dashscope_llm.stream_complete("How to make cake?")
for response in responses:
print(response.delta, end="")
Multi-Round Conversation
To have a conversation with the assistant and ask for a sugar-free cake recipe:
from llama_index.core.base.llms.types import MessageRole, ChatMessage
messages = [
ChatMessage(
role=MessageRole.SYSTEM, content="You are a helpful assistant."
),
ChatMessage(role=MessageRole.USER, content="How to make cake?"),
]
# Get first round response
resp = dashscope_llm.chat(messages)
print(resp)
# Continue conversation
messages.append(
ChatMessage(role=MessageRole.ASSISTANT, content=resp.message.content)
)
messages.append(
ChatMessage(role=MessageRole.USER, content="How to make it without sugar?")
)
# Get second round response
resp = dashscope_llm.chat(messages)
print(resp)
Handling Sugar-Free Recipes
For sugar-free cake recipes using honey as a sweetener:
resp = dashscope_llm.complete("How to make cake without sugar?")
print(resp)
LLM Implementation example
https://docs.llamaindex.ai/en/stable/examples/llm/dashscope/
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for llama_index_llms_dashscope-0.2.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c15de1f01fe1d1fb44d3680447dd6f90cf0f259eb95ec557f9d53602acad211 |
|
MD5 | 3e91fecfa16441a18889bb77a2551a7f |
|
BLAKE2b-256 | 1d59678e38ec687ed9eff053715574a8cc370aa2ad77991fc5a21115d676b36d |
Close
Hashes for llama_index_llms_dashscope-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96d58bd979983024f938cb17d2b07477d010bff41fb5406288e2e8f886f07ced |
|
MD5 | 2c18a22f3e261577e23f791ceba014f3 |
|
BLAKE2b-256 | 0b475ba00097bac8d2995ccd4a629942c07b5bfd58a75133da0deb00eb5c310c |