From 655024ffe1b3e75d76bf423f118604cf5d8d5540 Mon Sep 17 00:00:00 2001 From: shubham-tomar Date: Sun, 24 Aug 2025 11:07:57 +0530 Subject: [PATCH] bug fixes --- motia-content-creation/.gitignore | 6 +++- motia-content-creation/README.md | 1 + motia-content-creation/package.json | 13 ++++--- .../steps/generate-linkedin.step.py | 34 ++++++++++--------- .../steps/generate-twitter.step.py | 25 +++++++------- motia-content-creation/steps/scrape.step.py | 14 ++++---- 6 files changed, 51 insertions(+), 42 deletions(-) diff --git a/motia-content-creation/.gitignore b/motia-content-creation/.gitignore index c73bf6051..27ef17e38 100644 --- a/motia-content-creation/.gitignore +++ b/motia-content-creation/.gitignore @@ -106,4 +106,8 @@ temp/ .Spotlight-V100 .Trashes ehthumbs.db -Thumbs.db \ No newline at end of file +Thumbs.db + +# Typescript +types.d.ts +package-lock.json \ No newline at end of file diff --git a/motia-content-creation/README.md b/motia-content-creation/README.md index 6b4a3d6e9..d89effbb1 100644 --- a/motia-content-creation/README.md +++ b/motia-content-creation/README.md @@ -46,6 +46,7 @@ API → Scrape → Generate → Schedule 2. **Install project dependencies:** ```bash npm install or pnpm install + npx motia install ``` 3. **Configure environment:** diff --git a/motia-content-creation/package.json b/motia-content-creation/package.json index 99a0b44db..920440302 100644 --- a/motia-content-creation/package.json +++ b/motia-content-creation/package.json @@ -18,12 +18,15 @@ "web-scraping" ], "dependencies": { - "motia": "^0.4.0-beta.90", "@mendable/firecrawl-js": "^1.0.0", - "openai": "^4.90.0", + "axios": "^1.10.0", "dotenv": "^16.5.0", - "zod": "^3.25.67", - "axios": "^1.10.0" + "install": "^0.13.0", + "motia": "^0.4.0-beta.90", + "openai": "^4.90.0", + "or": "^0.2.0", + "pnpm": "^10.15.0", + "zod": "^3.25.67" }, "devDependencies": { "@types/node": "^20.17.28", @@ -31,4 +34,4 @@ "ts-node": "^10.9.2", "typescript": "^5.8.3" } -} \ No newline at end of file +} diff --git a/motia-content-creation/steps/generate-linkedin.step.py b/motia-content-creation/steps/generate-linkedin.step.py index 8d10d7546..1eede3548 100644 --- a/motia-content-creation/steps/generate-linkedin.step.py +++ b/motia-content-creation/steps/generate-linkedin.step.py @@ -1,15 +1,14 @@ import os import json +import ollama +import asyncio from pydantic import BaseModel, HttpUrl from datetime import datetime from dotenv import load_dotenv -from openai import AsyncOpenAI load_dotenv() -OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') - -openai_client = AsyncOpenAI(api_key=OPENAI_API_KEY) +OLLAMA_MODEL = os.getenv('OLLAMA_MODEL', 'deepseek-r1') class GenerateInput(BaseModel): requestId: str @@ -35,21 +34,24 @@ async def handler(input, context): linkedinPrompt = linkedinPromptTemplate.replace('{{title}}', input['title']).replace('{{content}}', input['content']) - context.logger.info("🔄 LinkedIn content generation started...") - - linkedin_content = await openai_client.chat.completions.create( - model="gpt-4o", - messages=[{'role': 'user', 'content': linkedinPrompt}], - temperature=0.7, - max_tokens=2000, - response_format={'type': 'json_object'} + context.logger.info(f"🔄 LinkedIn content generation started using Ollama model: {OLLAMA_MODEL}...") + response = ollama.chat( + model=OLLAMA_MODEL, + messages=[{'role': 'user', 'content': linkedinPrompt}], + options={ + 'temperature': 0.7, + 'num_predict': 2000 + } ) - + + response_content = response['message']['content'] + context.logger.info(f"Received raw response from Ollama: {response_content[:100]}...") + try: - linkedin_content = json.loads(linkedin_content.choices[0].message.content) + linkedin_content = json.loads(response['message']['content']) except Exception: - linkedin_content = {'text': linkedin_content.choices[0].message.content} - + linkedin_content = {'text': response['message']['content']} + context.logger.info(f"🎉 LinkedIn content generated successfully!") await context.emit({ diff --git a/motia-content-creation/steps/generate-twitter.step.py b/motia-content-creation/steps/generate-twitter.step.py index 6e9dbdf6a..2ec4fe38c 100644 --- a/motia-content-creation/steps/generate-twitter.step.py +++ b/motia-content-creation/steps/generate-twitter.step.py @@ -3,13 +3,11 @@ from pydantic import BaseModel, HttpUrl from datetime import datetime from dotenv import load_dotenv -from openai import AsyncOpenAI +import ollama load_dotenv() -OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') - -openai_client = AsyncOpenAI(api_key=OPENAI_API_KEY) +OLLAMA_MODEL = os.getenv('OLLAMA_MODEL', 'deepseek-r1') class GenerateInput(BaseModel): requestId: str @@ -37,18 +35,19 @@ async def handler(input, context): context.logger.info("🔄 Twitter content generation started...") - twitter_content = await openai_client.chat.completions.create( - model="gpt-4o", - messages=[{'role': 'user', 'content': twitterPrompt}], - temperature=0.7, - max_tokens=2000, - response_format={'type': 'json_object'} - ) + twitter_content = ollama.chat( + model=OLLAMA_MODEL, + messages=[{'role': 'user', 'content': twitterPrompt}], + options={ + 'temperature': 0.7, + 'num_predict': 2000 + } + ) try: - twitter_content = json.loads(twitter_content.choices[0].message.content) + twitter_content = json.loads(twitter_content['message']['content']) except Exception: - twitter_content = {'text': twitter_content.choices[0].message.content} + twitter_content = {'text': twitter_content['message']['content']} context.logger.info(f"🎉 Twitter content generated successfully!") diff --git a/motia-content-creation/steps/scrape.step.py b/motia-content-creation/steps/scrape.step.py index eaceafbc6..4c271e89f 100644 --- a/motia-content-creation/steps/scrape.step.py +++ b/motia-content-creation/steps/scrape.step.py @@ -1,6 +1,6 @@ import os from pydantic import BaseModel, HttpUrl -from firecrawl import FirecrawlApp +from firecrawl import Firecrawl from datetime import datetime from dotenv import load_dotenv @@ -26,15 +26,15 @@ class ScrapeInput(BaseModel): async def handler(input, context): context.logger.info(f"🕷️ Scraping article: {input['url']}") - app = FirecrawlApp(api_key=FIRECRAWL_API_KEY) + firecrawl = Firecrawl(api_key=FIRECRAWL_API_KEY) - scrapeResult = app.scrape_url(input['url']) + scrapeResult = firecrawl.scrape(input['url'], formats=["markdown"]) - if not scrapeResult.success: - raise Exception(f"Firecrawl scraping failed: {scrapeResult.error}") + if not hasattr(scrapeResult, 'markdown'): + raise Exception(f"Firecrawl scraping failed: No content returned") - content = scrapeResult.markdown - title = scrapeResult.metadata.get('title', 'Untitled Article') + content = scrapeResult.markdown or '' + title = getattr(scrapeResult.metadata, 'title', 'Untitled Article') if hasattr(scrapeResult, 'metadata') else 'Untitled Article' context.logger.info(f"✅ Successfully scraped: {title} ({len(content) if content else 0} characters)")