Skip to content

Commit fc20d9c

Browse files
committed
Final touches
1 parent 6df65c2 commit fc20d9c

12 files changed

Lines changed: 66 additions & 86 deletions

File tree

Dogefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ workers:
117117
tag: "chatf-1315"
118118
envs: [ django, ray_worker ]
119119
instance_size: XL
120-
command: /.venv/bin/ray start --address=back:6375 --resources='{"tasks":100,"rags":100}' --block
120+
command: /.venv/bin/ray start --address=back:6375 --resources='{"tasks":100,"ai_components":100}' --block
121121

122122
jobs:
123123
- name: migrate

back/back/apps/broker/admin.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
from .models.message import Message, UserFeedback, AdminReview
44
from .models import ConsumerRoundRobinQueue, RemoteSDKParsers
5+
import time
56

67

78
class MessageAdmin(admin.ModelAdmin):
89
list_display = ["conversation_id", "id", "sender_type", "payload_text", "created_date"]
910

1011
def payload_text(self, obj):
1112
payload = obj.stack[0]['payload']
12-
if isinstance(payload, str):
13-
return payload
14-
else:
13+
if 'content' in payload and isinstance(payload, dict):
1514
return payload['content']
15+
elif 'model_response' in payload and isinstance(payload, dict):
16+
return payload['model_response']
17+
else:
18+
return payload
1619

1720
def sender_type(self, obj):
1821
return obj.sender["type"]

back/back/apps/broker/models/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def get_formatted_conversation(self, chain):
125125
messages.append({"role": "assistant", "content": bot_content})
126126
bot_content = ""
127127

128-
messages.append({"role": "user", "content": m.stack[0]["payload"]})
128+
messages.append({"role": "user", "content": m.stack[0]["payload"]["content"]})
129129
human_messages_ids.append(m.id)
130130
elif m.sender["type"] == "bot":
131131
bot_content += m.stack[0]["payload"]["content"]

back/back/apps/language_model/ray_deployments/colbert_deployment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ray_actor_options={
1515
"num_cpus": 1,
1616
"resources": {
17-
"rags": 1,
17+
"ai_components": 1,
1818
},
1919
},
2020
)

back/back/apps/language_model/ray_deployments/e5_deployment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
ray_actor_options={
1414
"num_cpus": 1,
1515
"resources": {
16-
"rags": 1,
16+
"ai_components": 1,
1717
}
1818
}
1919
)

back/back/apps/language_model/ray_deployments/llm_deployment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ray_actor_options={
1111
"num_cpus": 0.01,
1212
"resources": {
13-
"rags": 1,
13+
"ai_components": 1,
1414
},
1515
},
1616
)

back/back/apps/language_model/tasks/intent_tasks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ def generate_suggested_intents_task(
393393
Message.objects.filter(
394394
stack__contains=[
395395
{
396-
"payload": {"references": {"knowledge_base_id": "1"}},
397-
"type": "rag_generated_text",
396+
"payload": {"references": {"knowledge_base_id": knowledge_base_pk}},
397+
# "type": "rag_generated_text",
398398
}
399399
]
400400
)
@@ -410,7 +410,7 @@ def generate_suggested_intents_task(
410410
messages = Message.objects.filter(id__in=Subquery(subquery), sender__type="human")
411411

412412
messages_text = [
413-
Message.objects.get(id=item["message_id"]).stack[0]["payload"]
413+
Message.objects.get(id=item["message_id"]).stack[0]["payload"]["content"]
414414
for item in messages
415415
]
416416

@@ -428,7 +428,7 @@ def generate_suggested_intents_task(
428428
< new_intents_thresholds["max"]
429429
]
430430

431-
messages_text = [message.stack[0]["payload"] for message in messages]
431+
messages_text = [message.stack[0]["payload"]["content"] for message in messages]
432432

433433
logger.info(f"Number of messages after filtering: {messages.count()}")
434434

back/run_ray_worker.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export $(xargs <.env)
22

3-
ray start --address=back:6375 --resources='{"tasks": 100, "rags": 100}' --block
3+
ray start --address=back:6375 --resources='{"tasks": 100, "ai_components": 100}' --block

doc/source/installation.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The system comprises seven main components, here are their relationships and the
1717
- **Widget** <!-- (<a href="/en/latest/modules/installations/index.html#widget-installation">local install</a>) --> is a JS browser client application from which the user interacts with the bot.
1818

1919

20-
- **Admin** is a JS browser client application to manage the chatbots, datasets, retriever, models, RAG configs, etc...
20+
- **Admin** is a JS browser client application to manage the chatbots, knowledge bases, retrievers, models, configs, etc...
2121

2222

2323
- **Ray workers** are used to run distributed inference on the models.
@@ -26,7 +26,7 @@ The system comprises seven main components, here are their relationships and the
2626
- **Channel layer** (Redis) is used to communicate through WebSockets between the back-end and the SDK, admin and widget.
2727

2828

29-
- **Relational Database** (PostgreSQL) is used to store all the data related to the chatbots, datasets, retriever, models, RAG configs, etc...
29+
- **Relational Database** (PostgreSQL) is used to store all the data related to the chatbots, knowledge bases, retrievers, models, configs, etc...
3030

3131
### Docker
3232

@@ -101,8 +101,6 @@ To manage the chatbot and view the model configuration we provided, go to:
101101

102102
## Deeper into ChatFAQ
103103

104-
If you want to use your own dataset, you can check the [Dataset Configuration](./modules/configuration/index.html#knowledge-base) documentation.
105-
106-
If you want to learn how to configure your own RAG (LLM model, retriever model, prompt configuration, etc...) you can check the [RAG Configuration](./modules/configuration/index.html#rag-config) documentation.
104+
If you want to use your own knowledge base, you can check the [Knowledge Base Configuration](./modules/configuration/index.html#knowledge-base) documentation.
107105

108106
If you want to learn how to use the SDK, so you can create your own chatbot behavior, you can check the [SDK](./modules/sdk/index.md) documentation.

doc/source/modules/configuration/index.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Here you can set up multiple AI components that can be used to compose AI pipeli
3232
- You can create a simple chatbot using only a LLM.
3333
- You can create any kind of agent using the ChatFAQ SDK, given that the backend supports tool use.
3434

35-
ChatFAQ provide in its fixtures a default configuration for each of these components except for the Knowledge Base and the RAG Config. You can apply the fixtures by simply running the following command from the `back` directory:
35+
ChatFAQ provide in its fixtures a default configuration for each of these components. You can apply the fixtures by simply running the following command from the `back` directory:
3636

3737
```bash
3838
make apply_fixtures
@@ -327,7 +327,7 @@ from chatfaq_sdk import ChatFAQSDK
327327
from chatfaq_sdk.clients import retrieve
328328
from chatfaq_sdk.layers import Message
329329

330-
async def send_rag_answer(sdk: ChatFAQSDK, ctx: dict):
330+
async def send_retrieval(sdk: ChatFAQSDK, ctx: dict):
331331
query = 'What is ChatFAQ?'
332332
items = await retrieve(sdk, 'chatfaq_retriever', query, top_k=3, bot_channel_name=ctx["bot_channel_name"])
333333
yield Message(
@@ -344,7 +344,7 @@ from chatfaq_sdk import ChatFAQSDK
344344
from chatfaq_sdk.clients import llm_request
345345
from chatfaq_sdk.layers import Message, StreamingMessage
346346

347-
async def send_rag_answer(sdk: ChatFAQSDK, ctx: dict):
347+
async def send_llm_answer(sdk: ChatFAQSDK, ctx: dict):
348348
# Some messages
349349
messages = [
350350
{"role": "system", "content": "You are a helpful assistant."},
@@ -353,3 +353,38 @@ async def send_rag_answer(sdk: ChatFAQSDK, ctx: dict):
353353
generator = llm_request(sdk, 'gpt-4o', use_conversation_context=False, conversation_id=ctx["conversation_id"], bot_channel_name=ctx["bot_channel_name"], messages=messages)
354354
yield StreamingMessage(generator)
355355
```
356+
357+
Creating a simple RAG pipeline:
358+
359+
```python
360+
from chatfaq_sdk import ChatFAQSDK
361+
from chatfaq_sdk.clients import retrieve, llm_request
362+
from chatfaq_sdk.layers import Message, StreamingMessage
363+
from chatfaq_sdk.utils import convert_mml_to_llm_format
364+
365+
async def send_rag_answer(sdk: ChatFAQSDK, ctx: dict):
366+
367+
messages = convert_mml_to_llm_format(ctx["conv_mml"][1:])
368+
last_user_message = messages[-1]["content"]
369+
370+
# Retrieve context
371+
contexts = await retrieve(sdk, 'chatfaq_retriever', last_user_message, top_k=3, bot_channel_name=ctx["bot_channel_name"])
372+
373+
# Augment prompt with context
374+
system_prompt = rag_system_prompt
375+
context_content = "\n".join([f"- {context['content']}" for context in contexts['knowledge_items']])
376+
system_prompt += f"\nInformation:\n{context_content}"
377+
messages.insert(0, {"role": "system", "content": system_prompt})
378+
379+
# Generate response
380+
generator = llm_request(
381+
sdk,
382+
"gpt-4o",
383+
use_conversation_context=False,
384+
conversation_id=ctx["conversation_id"],
385+
bot_channel_name=ctx["bot_channel_name"],
386+
messages=messages,
387+
)
388+
389+
yield StreamingMessage(generator, references=contexts)
390+
```

0 commit comments

Comments
 (0)