Skip to main content

LangChain integration for DeepAI API with chat, images, and speech capabilities

Project description

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:

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

langchain_deepai-0.0.2.tar.gz (32.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_deepai-0.0.2-py3-none-any.whl (28.0 kB view details)

Uploaded Python 3

File details

Details for the file langchain_deepai-0.0.2.tar.gz.

File metadata

  • Download URL: langchain_deepai-0.0.2.tar.gz
  • Upload date:
  • Size: 32.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for langchain_deepai-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c30fc0e74e2e44b18cca5eacb377ef2d5aab60a67d75adb67494de6bc334213e
MD5 157ffc9039497515fa5f0f3b4e0b6a3d
BLAKE2b-256 9e4a2946f9fb124add425c41ef0687557dea9342dabf64b1d531215d75cef663

See more details on using hashes here.

File details

Details for the file langchain_deepai-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_deepai-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cb231908f3ed7857b3d612e8ea451ef7aebebbc4440d503a28eea67087e8d817
MD5 cc518dcae82a75711ceedc94e44bc38e
BLAKE2b-256 0ef3f27476881c8299328827b01665534686364e6cd461b945e696918353a6a5

See more details on using hashes here.

Supported by

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