Skip to content

Commit

Permalink
Merge pull request #35 from jeffwiegner/patch-chat_id
Browse files Browse the repository at this point in the history
Getting chat_id to use as session_id
  • Loading branch information
coleam00 authored Jan 14, 2025
2 parents f1c4ef0 + a91332a commit 1879176
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions local-ai-packaged/n8n_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
import time
import requests

def extract_event_info(event_emitter) -> tuple[Optional[str], Optional[str]]:
if not event_emitter or not event_emitter.__closure__:
return None, None
for cell in event_emitter.__closure__:
if isinstance(request_info := cell.cell_contents, dict):
chat_id = request_info.get("chat_id")
message_id = request_info.get("message_id")
return chat_id, message_id
return None, None

class Pipe:
class Valves(BaseModel):
Expand Down Expand Up @@ -75,7 +84,7 @@ async def pipe(
await self.emit_status(
__event_emitter__, "info", "/Calling N8N Workflow...", False
)

chat_id, _ = extract_event_info(__event_emitter__)
messages = body.get("messages", [])

# Verify a message is available
Expand All @@ -87,7 +96,7 @@ async def pipe(
"Authorization": f"Bearer {self.valves.n8n_bearer_token}",
"Content-Type": "application/json",
}
payload = {"sessionId": f"{__user__['id']} - {messages[0]['content'].split('Prompt: ')[-1][:100]}"}
payload = {"sessionId": f"{chat_id}"}
payload[self.valves.input_field] = question
response = requests.post(
self.valves.n8n_url, json=payload, headers=headers
Expand Down

0 comments on commit 1879176

Please sign in to comment.