Skip to main content

LangChain DeepAI Integration

Python 3.8+ License: MIT

A comprehensive LangChain integration for the DeepAI API, providing seamless access to chat completions, image generation, text-to-speech, and speech-to-text capabilities.

Features

  • 🗨️ Chat Completions: Multiple specialized models for different use cases
  • 🎨 Image Generation: Text-to-image with various artistic styles
  • 🔊 Text-to-Speech: High-quality speech synthesis
  • 🎤 Speech-to-Text: Accurate audio transcription
  • ⚡ LangChain Compatible: Full integration with LangChain ecosystem
  • 🔄 Async Support: Both sync and async operations
  • 📚 Well Documented: Comprehensive docstrings and examples

Installation

pip install langchain-deepai

For image processing capabilities:

pip install langchain-deepai[image]

For development:

pip install langchain-deepai[dev]

Quick Start

1. Set up your API key

export DEEPAI_API_KEY="your-deepai-api-key"

Or pass it directly:

from langchain_deepai import ChatDeepAI

chat = ChatDeepAI(api_key="your-deepai-api-key")

2. Basic Chat Example

from langchain_deepai import ChatDeepAI
from langchain_core.messages import HumanMessage

# Initialize chat model
chat = ChatDeepAI(
    model="standard",
    chat_style="chatgpt-alternative"
)

# Send a message
messages = [HumanMessage(content="Hello! How are you today?")]
response = chat.invoke(messages)
print(response.content)

3. Specialized Models

from langchain_deepai import DeepAIMath, DeepAICode

# Math-focused model
math_chat = DeepAIMath()
response = math_chat.invoke([HumanMessage(content="Solve: 2x + 5 = 15")])

# Code-focused model  
code_chat = DeepAICode()
response = code_chat.invoke([HumanMessage(content="Write a Python sorting function")])

4. Image Generation

from langchain_deepai import ImageDeepAI, generate_image

# Using the class
image_gen = ImageDeepAI()
result = image_gen.generate(
    prompt="A beautiful sunset over mountains",
    model="text2img",
    width=512,
    height=512
)

# Save the image
with open("sunset.jpg", "wb") as f:
    f.write(result["image_data"])

# Using the convenience function
result = generate_image(
    prompt="A cyberpunk cityscape at night",
    model="cyberpunk-generator"
)

5. Text-to-Speech

from langchain_deepai import TextToSpeechDeepAI

tts = TextToSpeechDeepAI()
result = tts.synthesize(
    text="Hello, this is a test of text-to-speech synthesis.",
    voice="default"
)

# Save audio file
with open("speech.wav", "wb") as f:
    f.write(result["audio_data"])

6. Speech-to-Text

from langchain_deepai import SpeechToTextDeepAI

stt = SpeechToTextDeepAI()
result = stt.transcribe("audio_file.wav")
print(result["text"])

Available Models and Styles

Chat Models

  • standard: General purpose conversational AI
  • math: Mathematics and problem-solving specialized
  • online: Web-aware model with current information
  • code: Programming and development focused

Chat Styles

  • chatgpt-alternative: Default conversational style
  • ai-code: Programming focused
  • mathematics: Mathematical reasoning
  • professional: Business communication
  • creative: Creative and imaginative
  • casual: Relaxed and informal
  • goku: Enthusiastic character style
  • gojo_9: Confident character style

Image Models

  • text2img: Standard text-to-image generation
  • fantasy-world-generator: Fantasy themed
  • cyberpunk-generator: Cyberpunk aesthetic
  • renaissance-painting-generator: Renaissance art style
  • abstract-painting-generator: Abstract art
  • impressionism-painting-generator: Impressionist style

Advanced Usage

Custom Chat Styles

from langchain_deepai import ChatDeepAI

# Professional business communication
business_chat = ChatDeepAI(
    model="standard",
    chat_style="professional"
)

# Creative writing assistant
creative_chat = ChatDeepAI(
    model="standard", 
    chat_style="creative"
)

Session Management

chat = ChatDeepAI()
chat.set_session_id("user-123")

# All messages in this session will maintain context
response1 = chat.invoke([HumanMessage(content="My name is Alice")])
response2 = chat.invoke([HumanMessage(content="What's my name?")])

Error Handling

from langchain_deepai import ChatDeepAI
import logging

# Enable logging
logging.basicConfig(level=logging.INFO)

try:
    chat = ChatDeepAI(api_key="invalid-key")
    response = chat.invoke([HumanMessage(content="Hello")])
except Exception as e:
    print(f"Error: {e}")

Batch Processing

from langchain_deepai import ImageDeepAI

image_gen = ImageDeepAI()

prompts = [
    "A serene lake at sunrise",
    "A bustling city street at night", 
    "A peaceful forest in autumn"
]

results = image_gen.generate_multiple(prompts, model="text2img")

Integration with LangChain

Chains

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_deepai import ChatDeepAI

# Create a chain
prompt = PromptTemplate(
    input_variables=["topic"],
    template="Write a short story about {topic}"
)

chain = LLMChain(
    llm=ChatDeepAI(chat_style="creative"),
    prompt=prompt
)

result = chain.run(topic="time travel")

Agents

from langchain.agents import initialize_agent, Tool
from langchain_deepai import ChatDeepAI, ImageDeepAI

def generate_image_tool(prompt: str) -> str:
    image_gen = ImageDeepAI()
    result = image_gen.generate(prompt)
    return f"Image generated and saved. URL: {result['image_url']}"

tools = [
    Tool(
        name="ImageGenerator",
        func=generate_image_tool,
        description="Generate images from text descriptions"
    )
]

agent = initialize_agent(
    tools=tools,
    llm=ChatDeepAI(),
    agent="zero-shot-react-description"
)

API Reference

ChatDeepAI

Main chat model class with full LangChain compatibility.

Parameters:

  • model_name: Model to use ("standard", "math", "online", "code")
  • chat_style: Conversation style
  • api_key: DeepAI API key
  • max_tokens: Maximum tokens to generate
  • temperature: Response randomness
  • session_id: Session identifier for context

Specialized Models

  • DeepAIMath: Mathematics-focused chat model
  • DeepAICode: Programming-focused chat model
  • DeepAIOnline: Web-aware chat model
  • DeepAIProfessional: Business communication model
  • DeepAICreative: Creative writing model
  • DeepAICasual: Casual conversation model

ImageDeepAI

Image generation with multiple artistic styles.

Methods:

  • generate(): Generate single image
  • generate_multiple(): Generate multiple images
  • generate_and_save(): Generate and save to file

TextToSpeechDeepAI

High-quality text-to-speech synthesis.

Methods:

  • synthesize(): Convert text to speech
  • synthesize_and_save(): Convert and save audio
  • synthesize_multiple(): Batch TTS processing

SpeechToTextDeepAI

Accurate speech-to-text transcription.

Methods:

  • transcribe(): Transcribe audio to text
  • transcribe_multiple(): Batch STT processing

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For support and questions:

Metadata

Release files for langchain-deepai 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-deepai 0.0.6
File Size Uploaded Provenance
langchain_deepai-0.0.6.tar.gz 35.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-deepai 0.0.6
File Interpreter ABI Platform
langchain_deepai-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 71.8 kB

Release files / langchain_deepai-0.0.6.tar.gz

Download URL langchain_deepai-0.0.6.tar.gz
Size 35.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b7f1ae61efc99b30e298ced124e76bf93c89b5185916c328a2a29fd05ac72522
BLAKE2b-256 checksum
How to use checksums
42c62de747661511b70166bc44002b1ee0bbb7f5db680c3d223a5c3c8d00fbce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / langchain_deepai-0.0.6-py3-none-any.whl

Download URL langchain_deepai-0.0.6-py3-none-any.whl
Size 36.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca3c44671425888a88fc93f23b8230b3659843b64607acd3b49795372ddf2ec8
BLAKE2b-256 checksum
How to use checksums
80f73ae45bb490ca1a6d03c74a25296260103cbd63c51f848efc6fe42bde90a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Security for langchain-deepai 0.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page