forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Examples] Add mem0 example (BasedHardware#479)
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
|
||
from fastapi import FastAPI | ||
from fastapi.templating import Jinja2Templates | ||
from modal import Image, App, Secret, asgi_app, mount | ||
|
||
from models import Memory | ||
from mem0 import MemoryClient | ||
|
||
app = FastAPI() | ||
|
||
modal_app = App( | ||
name="plugins_examples", | ||
secrets=[Secret.from_dotenv(".env")], | ||
mounts=[ | ||
mount.Mount.from_local_dir("templates/", remote_path="templates/"), | ||
], | ||
) | ||
|
||
mem0 = MemoryClient(api_key=os.getenv("MEM0_API_KEY", "123")) | ||
|
||
|
||
@modal_app.function( | ||
image=Image.debian_slim().pip_install_from_requirements("requirements.txt"), | ||
keep_warm=1, # need 7 for 1rps | ||
memory=(1024, 2048), | ||
cpu=4, | ||
allow_concurrent_inputs=10, | ||
) | ||
@asgi_app() | ||
def plugins_app(): | ||
return app | ||
|
||
|
||
# ************************************************** | ||
# ************ On Memory Created Plugin ************ | ||
# ************************************************** | ||
|
||
|
||
@app.post("/mem0") | ||
def mem0_add(memory: Memory, uid: str): | ||
transcript_segments = memory.transcriptSegments | ||
messages = [] | ||
for segment in transcript_segments: | ||
messages.append( | ||
{ | ||
"role": "user" if segment.is_user else "assistant", | ||
"content": segment.text, | ||
} | ||
) | ||
if not messages: | ||
return {"message": "No messages found"} | ||
|
||
mem0.add(messages, user_id=uid) | ||
memories = mem0.search(messages, user_id=uid) | ||
response = [row["memory"] for row in memories] | ||
response_str = "\n".join(response) | ||
return {"message": f"User memories: {response_str}"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from datetime import datetime | ||
from typing import List, Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Structured(BaseModel): | ||
title: str | ||
overview: str | ||
emoji: str = "" | ||
category: str = "other" | ||
|
||
|
||
class ActionItem(BaseModel): | ||
description: str | ||
|
||
|
||
class Event(BaseModel): | ||
title: str | ||
startsAt: datetime | ||
duration: int | ||
description: Optional[str] = "" | ||
created: bool = False | ||
|
||
|
||
class MemoryPhoto(BaseModel): | ||
base64: str | ||
description: str | ||
|
||
|
||
class PluginResponse(BaseModel): | ||
pluginId: Optional[str] = None | ||
content: str | ||
|
||
|
||
class TranscriptSegment(BaseModel): | ||
text: str | ||
speaker: str | ||
speaker_id: int | ||
is_user: bool | ||
start: float | ||
end: float | ||
|
||
|
||
class Memory(BaseModel): | ||
createdAt: datetime | ||
startedAt: Optional[datetime] = None | ||
finishedAt: Optional[datetime] = None | ||
transcript: str = "" | ||
transcriptSegments: List[TranscriptSegment] = [] | ||
photos: Optional[List[MemoryPhoto]] = [] | ||
recordingFilePath: Optional[str] = None | ||
recordingFileBase64: Optional[str] = None | ||
structured: Structured | ||
pluginsResponse: List[PluginResponse] = [] | ||
discarded: bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mem0ai>=0.0.9 | ||
modal==0.63.75 | ||
fastapi==0.111.0 | ||
fastapi-cli==0.0.4 | ||
modal==0.63.75 | ||
openai==1.35.14 | ||
requests==2.31.0 |