Skip to main content

llama-index llms dashscope integration

Project description

LlamaIndex Llms Integration: Dashscope

Installation

  1. Install the required Python package:

    pip install llama-index-llms-dashscope
    
  2. 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

llama_index_llms_dashscope-0.2.6.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file llama_index_llms_dashscope-0.2.6.tar.gz.

File metadata

  • Download URL: llama_index_llms_dashscope-0.2.6.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for llama_index_llms_dashscope-0.2.6.tar.gz
Algorithm Hash digest
SHA256 9df36ac02d32a217f50a6556277bc4a74eb6265cf899217baf671a0b4a2ba105
MD5 243d110289cdc13defda670d704e4591
BLAKE2b-256 2875cc9a10a592db42cc803b1378c57e9bb6d3fd34319b0bd49006201c0bda64

See more details on using hashes here.

File details

Details for the file llama_index_llms_dashscope-0.2.6-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_llms_dashscope-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0a7eedcb701c5b8e5ae913f978d0197fd4d6f7e0a51a6d4a05bddd478cbe7d8d
MD5 b1ad29d35f76c9fb2f036e4c6ddce6d8
BLAKE2b-256 bf6222afe80d6c8b36f664b407c885f848ba3a25fea0fb6809bf47c0da2aa009

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page