xujfcn commited on
Commit
4eb0a6b
Β·
1 Parent(s): e91775a

LangChain + LlamaIndex + AutoGen + CrewAI integration guide

Browse files
Files changed (1) hide show
  1. README.md +297 -3
README.md CHANGED
@@ -1,3 +1,297 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - tutorial
5
+ - crazyrouter
6
+ - langchain
7
+ - llamaindex
8
+ - autogen
9
+ - ai-agents
10
+ - llm
11
+ language:
12
+ - en
13
+ - zh
14
+ ---
15
+
16
+ # πŸ”— Crazyrouter + LangChain / LlamaIndex / AutoGen
17
+
18
+ > Use 624+ AI models in your favorite framework β€” zero config changes.
19
+
20
+ Since Crazyrouter is 100% OpenAI-compatible, it works out of the box with every major AI framework.
21
+
22
+ ---
23
+
24
+ ## LangChain
25
+
26
+ ### Installation
27
+
28
+ ```bash
29
+ pip install langchain langchain-openai
30
+ ```
31
+
32
+ ### Basic Chat
33
+
34
+ ```python
35
+ from langchain_openai import ChatOpenAI
36
+
37
+ llm = ChatOpenAI(
38
+ base_url="https://crazyrouter.com/v1",
39
+ api_key="sk-your-crazyrouter-key",
40
+ model="gpt-4o"
41
+ )
42
+
43
+ response = llm.invoke("Explain microservices in one paragraph")
44
+ print(response.content)
45
+ ```
46
+
47
+ ### Switch Models on the Fly
48
+
49
+ ```python
50
+ # Use Claude for analysis
51
+ analyst = ChatOpenAI(
52
+ base_url="https://crazyrouter.com/v1",
53
+ api_key="sk-your-crazyrouter-key",
54
+ model="claude-sonnet-4-20250514"
55
+ )
56
+
57
+ # Use DeepSeek for coding (cheaper)
58
+ coder = ChatOpenAI(
59
+ base_url="https://crazyrouter.com/v1",
60
+ api_key="sk-your-crazyrouter-key",
61
+ model="deepseek-chat"
62
+ )
63
+
64
+ # Use GPT-4o-mini for simple tasks (cheapest)
65
+ helper = ChatOpenAI(
66
+ base_url="https://crazyrouter.com/v1",
67
+ api_key="sk-your-crazyrouter-key",
68
+ model="gpt-4o-mini"
69
+ )
70
+ ```
71
+
72
+ ### LangChain Chains
73
+
74
+ ```python
75
+ from langchain_core.prompts import ChatPromptTemplate
76
+ from langchain_core.output_parsers import StrOutputParser
77
+
78
+ prompt = ChatPromptTemplate.from_messages([
79
+ ("system", "You are a helpful coding assistant."),
80
+ ("user", "{question}")
81
+ ])
82
+
83
+ chain = prompt | llm | StrOutputParser()
84
+
85
+ result = chain.invoke({"question": "How do I read a CSV file in Python?"})
86
+ print(result)
87
+ ```
88
+
89
+ ### RAG with Crazyrouter
90
+
91
+ ```python
92
+ from langchain_openai import OpenAIEmbeddings, ChatOpenAI
93
+ from langchain_community.vectorstores import FAISS
94
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
95
+ from langchain_core.prompts import ChatPromptTemplate
96
+ from langchain_core.runnables import RunnablePassthrough
97
+
98
+ # Embeddings (use a cheap model)
99
+ embeddings = OpenAIEmbeddings(
100
+ base_url="https://crazyrouter.com/v1",
101
+ api_key="sk-your-crazyrouter-key",
102
+ model="text-embedding-3-small"
103
+ )
104
+
105
+ # Chat model
106
+ llm = ChatOpenAI(
107
+ base_url="https://crazyrouter.com/v1",
108
+ api_key="sk-your-crazyrouter-key",
109
+ model="gpt-4o-mini"
110
+ )
111
+
112
+ # Split your documents
113
+ splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
114
+ docs = splitter.create_documents(["Your document text here..."])
115
+
116
+ # Create vector store
117
+ vectorstore = FAISS.from_documents(docs, embeddings)
118
+ retriever = vectorstore.as_retriever()
119
+
120
+ # RAG chain
121
+ template = """Answer based on context:
122
+ {context}
123
+
124
+ Question: {question}"""
125
+
126
+ prompt = ChatPromptTemplate.from_template(template)
127
+
128
+ chain = (
129
+ {"context": retriever, "question": RunnablePassthrough()}
130
+ | prompt
131
+ | llm
132
+ )
133
+
134
+ result = chain.invoke("What does the document say?")
135
+ print(result.content)
136
+ ```
137
+
138
+ ---
139
+
140
+ ## LlamaIndex
141
+
142
+ ### Installation
143
+
144
+ ```bash
145
+ pip install llama-index llama-index-llms-openai-like
146
+ ```
147
+
148
+ ### Basic Usage
149
+
150
+ ```python
151
+ from llama_index.llms.openai_like import OpenAILike
152
+
153
+ llm = OpenAILike(
154
+ api_base="https://crazyrouter.com/v1",
155
+ api_key="sk-your-crazyrouter-key",
156
+ model="gpt-4o",
157
+ is_chat_model=True
158
+ )
159
+
160
+ response = llm.complete("What is retrieval augmented generation?")
161
+ print(response)
162
+ ```
163
+
164
+ ### With OpenAI Class Directly
165
+
166
+ ```python
167
+ from llama_index.llms.openai import OpenAI
168
+ import os
169
+
170
+ os.environ["OPENAI_API_KEY"] = "sk-your-crazyrouter-key"
171
+ os.environ["OPENAI_API_BASE"] = "https://crazyrouter.com/v1"
172
+
173
+ llm = OpenAI(model="gpt-4o-mini")
174
+ response = llm.complete("Hello!")
175
+ print(response)
176
+ ```
177
+
178
+ ---
179
+
180
+ ## AutoGen
181
+
182
+ ### Installation
183
+
184
+ ```bash
185
+ pip install autogen-agentchat
186
+ ```
187
+
188
+ ### Multi-Agent Setup
189
+
190
+ ```python
191
+ import autogen
192
+
193
+ config_list = [
194
+ {
195
+ "model": "gpt-4o",
196
+ "base_url": "https://crazyrouter.com/v1",
197
+ "api_key": "sk-your-crazyrouter-key",
198
+ }
199
+ ]
200
+
201
+ llm_config = {"config_list": config_list}
202
+
203
+ # Create agents
204
+ assistant = autogen.AssistantAgent(
205
+ name="assistant",
206
+ llm_config=llm_config,
207
+ )
208
+
209
+ user_proxy = autogen.UserProxyAgent(
210
+ name="user_proxy",
211
+ human_input_mode="NEVER",
212
+ max_consecutive_auto_reply=3,
213
+ code_execution_config={"work_dir": "coding"},
214
+ )
215
+
216
+ # Start conversation
217
+ user_proxy.initiate_chat(
218
+ assistant,
219
+ message="Write a Python script that fetches the top 10 Hacker News stories."
220
+ )
221
+ ```
222
+
223
+ ### Multi-Model Agents (Cost Optimization)
224
+
225
+ ```python
226
+ # Expensive model for complex reasoning
227
+ senior_config = [{"model": "gpt-4o", "base_url": "https://crazyrouter.com/v1", "api_key": "sk-your-key"}]
228
+
229
+ # Cheap model for simple tasks
230
+ junior_config = [{"model": "gpt-4o-mini", "base_url": "https://crazyrouter.com/v1", "api_key": "sk-your-key"}]
231
+
232
+ senior = autogen.AssistantAgent("senior", llm_config={"config_list": senior_config})
233
+ junior = autogen.AssistantAgent("junior", llm_config={"config_list": junior_config})
234
+ ```
235
+
236
+ ---
237
+
238
+ ## CrewAI
239
+
240
+ ```python
241
+ from crewai import Agent, Task, Crew
242
+ from langchain_openai import ChatOpenAI
243
+
244
+ llm = ChatOpenAI(
245
+ base_url="https://crazyrouter.com/v1",
246
+ api_key="sk-your-crazyrouter-key",
247
+ model="gpt-4o"
248
+ )
249
+
250
+ researcher = Agent(
251
+ role="Researcher",
252
+ goal="Find accurate information",
253
+ backstory="You are an expert researcher.",
254
+ llm=llm
255
+ )
256
+
257
+ task = Task(
258
+ description="Research the latest trends in AI API gateways",
259
+ agent=researcher,
260
+ expected_output="A summary of trends"
261
+ )
262
+
263
+ crew = Crew(agents=[researcher], tasks=[task])
264
+ result = crew.kickoff()
265
+ print(result)
266
+ ```
267
+
268
+ ---
269
+
270
+ ## Environment Variables (Works Everywhere)
271
+
272
+ Set these once and most frameworks auto-detect:
273
+
274
+ ```bash
275
+ export OPENAI_API_KEY="sk-your-crazyrouter-key"
276
+ export OPENAI_API_BASE="https://crazyrouter.com/v1"
277
+ export OPENAI_BASE_URL="https://crazyrouter.com/v1"
278
+ ```
279
+
280
+ ---
281
+
282
+ ## Pro Tips
283
+
284
+ 1. **Use cheap models for agents that do simple tasks** β€” `gpt-4o-mini` or `deepseek-chat` for routing, summarizing, formatting
285
+ 2. **Use powerful models for reasoning** β€” `gpt-4o`, `claude-sonnet-4-20250514`, or `deepseek-reasoner` for complex analysis
286
+ 3. **Mix providers freely** β€” one agent uses Claude, another uses GPT, another uses Gemini. All through one API key
287
+ 4. **Monitor costs** β€” Crazyrouter dashboard shows per-model usage
288
+
289
+ ---
290
+
291
+ ## Links
292
+
293
+ - 🌐 [Crazyrouter](https://crazyrouter.com)
294
+ - πŸ“– [Getting Started Guide](https://huggingface.co/xujfcn/Crazyrouter-Getting-Started)
295
+ - πŸ€– [Live Demo](https://huggingface.co/spaces/xujfcn/Crazyrouter-Demo)
296
+ - πŸ’¬ [Telegram](https://t.me/crazyrouter)
297
+ - 🐦 [Twitter @metaviiii](https://twitter.com/metaviiii)