> ## Documentation Index
> Fetch the complete documentation index at: https://veniceai-docs-revamp.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# تكامل LangChain

> استخدم Venice AI مع LangChain للسلاسل والوكلاء وخطوط أنابيب RAG

يعمل Venice AI بسلاسة مع [LangChain](https://python.langchain.com/) بفضل التوافق الكامل مع OpenAI SDK. ابنِ السلاسل والوكلاء وخطوط أنابيب RAG ببنية تحتية تركز على الخصوصية من Venice.

## الإعداد

```bash theme={"dark"}
pip install langchain langchain-openai openai
```

## نماذج الدردشة

استخدم `ChatOpenAI` مع عنوان URL الأساسي لـ Venice:

```python theme={"dark"}
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="venice-uncensored-1-2",
    api_key="your-venice-api-key",
    base_url="https://api.venice.ai/api/v1",
    temperature=0.7,
)

response = llm.invoke("Explain privacy-preserving AI in 2 sentences.")
print(response.content)
```

## البث المباشر

```python theme={"dark"}
for chunk in llm.stream("Write a haiku about decentralization."):
    print(chunk.content, end="", flush=True)
```

## التضمينات

```python theme={"dark"}
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(
    model="text-embedding-bge-m3",
    api_key="your-venice-api-key",
    base_url="https://api.venice.ai/api/v1",
    check_embedding_ctx_length=False,  # مطلوب لـ Venice
)

vectors = embeddings.embed_documents([
    "Venice AI provides private inference.",
    "No data is retained after processing.",
])
print(f"Embedding dimension: {len(vectors[0])}")
```

## السلاسل

### سلسلة بسيطة مع قالب الموجّه

```python theme={"dark"}
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a {role}. Answer concisely."),
    ("user", "{question}"),
])

chain = prompt | llm
response = chain.invoke({"role": "privacy expert", "question": "Why does zero data retention matter?"})
print(response.content)
```

### سلسلة متسلسلة

```python theme={"dark"}
from langchain_core.output_parsers import StrOutputParser

# السلسلة 1: توليد ملخص للموضوع
summarizer = ChatPromptTemplate.from_messages([
    ("user", "Summarize this topic in 3 bullet points: {topic}")
]) | llm | StrOutputParser()

# السلسلة 2: توليد أسئلة من الملخص
questioner = ChatPromptTemplate.from_messages([
    ("user", "Based on this summary, generate 3 thought-provoking questions:\n{summary}")
]) | llm | StrOutputParser()

# التكوين
summary = summarizer.invoke({"topic": "decentralized AI inference"})
questions = questioner.invoke({"summary": summary})
print(questions)
```

## خط أنابيب RAG

ابنِ خط أنابيب توليد مدعوم بالاسترجاع مع Venice:

```python theme={"dark"}
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

# تهيئة نماذج Venice
llm = ChatOpenAI(
    model="zai-org-glm-5-1",
    api_key="your-venice-api-key",
    base_url="https://api.venice.ai/api/v1",
)

embeddings = OpenAIEmbeddings(
    model="text-embedding-bge-m3",
    api_key="your-venice-api-key",
    base_url="https://api.venice.ai/api/v1",
    check_embedding_ctx_length=False,
)

# تحميل المستندات وتقسيمها
documents = [
    "Venice AI provides private, uncensored AI inference with zero data retention.",
    "The Venice API is OpenAI-compatible, supporting chat completions, images, audio, video, and embeddings.",
    "Venice supports function calling, structured outputs, web search, and reasoning models.",
    "Privacy levels include Private (zero retention) and Anonymized (third-party processed).",
]

# إنشاء مخزن المتجهات
vectorstore = FAISS.from_texts(documents, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})

# موجّه RAG
rag_prompt = ChatPromptTemplate.from_messages([
    ("system", "Answer the question based only on the following context:\n\n{context}"),
    ("user", "{question}"),
])

# سلسلة RAG
def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | rag_prompt
    | llm
    | StrOutputParser()
)

answer = rag_chain.invoke("What privacy levels does Venice offer?")
print(answer)
```

## استدعاء الدوال مع الوكلاء

```python theme={"dark"}
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate

# استخدم نموذجاً قادراً على استدعاء الدوال
llm = ChatOpenAI(
    model="zai-org-glm-5-1",
    api_key="your-venice-api-key",
    base_url="https://api.venice.ai/api/v1",
)

@tool
def get_venice_model_price(model_id: str) -> str:
    """Get the pricing for a Venice AI model."""
    prices = {
        "venice-uncensored-1-2": "Input: $0.20/1M, Output: $0.90/1M",
        "zai-org-glm-5-1": "Input: $1.75/1M, Output: $5.50/1M",
        "qwen3-5-9b": "Input: $0.10/1M, Output: $0.15/1M",
    }
    return prices.get(model_id, f"Model {model_id} not found in price list.")

prompt = ChatPromptTemplate.from_messages([
    ("system", "You help users find the right Venice AI model. Use tools when needed."),
    ("placeholder", "{chat_history}"),
    ("user", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, [get_venice_model_price], prompt)
executor = AgentExecutor(agent=agent, tools=[get_venice_model_price], verbose=True)

result = executor.invoke({"input": "What's the cheapest Venice text model?", "chat_history": []})
print(result["output"])
```

## المخرجات المنظمة

```python theme={"dark"}
from pydantic import BaseModel, Field

class MovieReview(BaseModel):
    title: str = Field(description="Movie title")
    rating: float = Field(description="Rating out of 10")
    summary: str = Field(description="One-sentence summary")

structured_llm = llm.with_structured_output(MovieReview)
review = structured_llm.invoke("Review the movie Inception")
print(f"{review.title}: {review.rating}/10 — {review.summary}")
```

## تكامل البحث على الويب

استخدم بحث الويب المدمج في Venice عبر `venice_parameters`:

```python theme={"dark"}
from langchain_openai import ChatOpenAI

llm_with_search = ChatOpenAI(
    model="venice-uncensored",
    api_key="your-venice-api-key",
    base_url="https://api.venice.ai/api/v1",
    extra_body={
        "venice_parameters": {
            "enable_web_search": "auto"
        }
    }
)

response = llm_with_search.invoke("What are the latest developments in AI this week?")
print(response.content)
```

أو مرره لكل طلب:

```python theme={"dark"}
response = llm.invoke(
    "What are the latest developments in AI this week?",
    extra_body={"venice_parameters": {"enable_web_search": "auto"}}
)
```

## النماذج الموصى بها لـ LangChain

| حالة الاستخدام     | النموذج                          | السبب                        |
| ------------------ | -------------------------------- | ---------------------------- |
| السلاسل العامة     | `venice-uncensored`              | سريع، رخيص، غير خاضع للرقابة |
| التفكير المعقد     | `zai-org-glm-5-1`                | أفضل نموذج رائد خاص          |
| استدعاء الدوال     | `zai-org-glm-5-1`                | استخدام موثوق للأدوات        |
| الرؤية + النص      | `qwen3-vl-235b-a22b`             | فهم رؤية متقدم               |
| توليد الكود        | `qwen3-coder-480b-a35b-instruct` | محسن للكود                   |
| التضمينات (RAG)    | `text-embedding-bge-m3`          | تضمينات خاصة                 |
| اقتصادي / حجم عالٍ | `qwen3-5-9b`                     | \$0.10/1M للإدخال            |

<Card title="عرض جميع النماذج" icon="database" href="/models/overview">
  تصفح جميع نماذج Venice مع التسعير والقدرات
</Card>
