Skip to content

Commit

Permalink
Refactor/chatgpt prompt (vechain#5)
Browse files Browse the repository at this point in the history
* made chatgpt prompt more modularized

* changed validityFactor

* removed commented smart contract code

* implemented dropzone with props

* removed console.log
  • Loading branch information
zayLatt25 authored Sep 14, 2024
1 parent 5c4f4a9 commit 12a43f7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
20 changes: 6 additions & 14 deletions apps/backend/src/controllers/submission.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,19 @@ export class SubmissionController {

public submitReceipt = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const body: Omit<Submission, 'timestamp'> = req.body;
const body: Omit<Submission, 'timestamp' | 'promptType'> = req.body;

const submissionRequest: Submission = {
...body,
timestamp: Date.now(),
};
// Submission validation with smart contract
await this.contracts.validateSubmission(submissionRequest);

//const validationResult = await this.openai.validateImage(body.image);
// TODO: Submission validation with smart contract
const validationResult = await this.openai.validateImage(body.image, req.body.promptType);

//if (validationResult == undefined || !('validityFactor' in (validationResult as object))) {
// throw new HttpException(500, 'Error validating image');
//}

const validityFactor = 1;

if (validityFactor > 0.5) {
if (!(await this.contracts.registerSubmission(submissionRequest))) {
throw new HttpException(500, 'Error registering submission and sending rewards');
}
const validityFactor = validationResult['validityFactor'];
if (validityFactor < 0.5) {
throw new HttpException(500, 'Error registering submission and sending rewards');
}

res.status(200).json({ validation: { validityFactor: 1 } });
Expand Down
4 changes: 4 additions & 0 deletions apps/backend/src/dtos/submission.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ export class SubmitDto {
@IsString()
@IsNotEmpty()
public deviceID: string;

@IsString()
@IsNotEmpty()
public promptType: string;
}
1 change: 1 addition & 0 deletions apps/backend/src/interfaces/submission.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface Submission {
timestamp: number;
image?: string;
deviceID?: string;
promptType?: string;
}
33 changes: 27 additions & 6 deletions apps/backend/src/services/openai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,42 @@ import { Service } from 'typedi';

@Service()
export class OpenaiService {
public async validateImage(image: string): Promise<unknown> {
public async validateImage(image: string, promptType: string): Promise<unknown> {
if (!isBase64Image(image)) throw new HttpException(400, 'Invalid image format');

const prompt = `
let prompt = '';
switch (promptType) {
case 'Transport':
prompt = `
Analyze the image provided. The image MUST satisfy all of the following criteria:
1. It must have as subject a receipt of purchase of at least one product.
2. It must not be a screenshot.
3. It must include the date of the purchase.
4. It must include the name of the store where the purchase was made.
1. It must be a screenshot or picture of public transport including bikes, and buses
2. It must include the distance travelled
Please respond always and uniquely with the following JSON object as you are REST API that returns the following object:
{
"validityFactor": {validityFactorNumber}, // 0-1, 1 if it satisfies all the criteria, 0 otherwise
"descriptionOfAnalysis": "{analysis}", // indicate your analysis of the image and why it satisfies or not the criteria. The analysis will be shown to the user so make him understand why the image doesn't satisfy the criteria if it doesn't without going into detail on exact criteria. Remember we are rewarding users that drink coffee in a sustainable way.
}
`;
break;

case 'Electricity':
break;

case 'Packaging':
break;

case 'Trees':
break;

case 'Trash':
break;

case 'Volunteering':
break;

default:
throw new HttpException(400, 'Invalid prompt type');
}

const gptResponse = await openAIHelper.askChatGPTAboutImage({
base64Image: image,
Expand Down
5 changes: 3 additions & 2 deletions apps/frontend/src/components/Dropzone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useWallet } from "@vechain/dapp-kit-react";
import { submitReceipt } from "../networking";
import { useDisclosure, useSubmission } from "../hooks";

export const Dropzone = () => {
export const Dropzone = ({ promptType }: { promptType: string }) => {
const { account } = useWallet();

const { setIsLoading, setResponse } = useSubmission();
Expand Down Expand Up @@ -51,6 +51,7 @@ export const Dropzone = () => {
address: account,
deviceID,
image: base64Image,
promptType,
});

console.log(response);
Expand All @@ -62,7 +63,7 @@ export const Dropzone = () => {
setIsLoading(false);
}
},
[account, onOpen, setIsLoading, setResponse],
[account, onOpen, setIsLoading, setResponse]
);

return (
Expand Down
1 change: 1 addition & 0 deletions apps/frontend/src/networking/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface ReceiptData {
image: string;
address: string;
deviceID: string;
promptType: string;
}
4 changes: 2 additions & 2 deletions apps/frontend/src/routes/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Link } from "react-router-dom";
import "./form.css";

export default function Form({ type }: { type: "reduce" | "offset" }) {
const [category, setCategory] = useState("All");
const [category, setCategory] = useState("Transport");

// Define categories based on the type prop
const categories =
Expand Down Expand Up @@ -65,7 +65,7 @@ export default function Form({ type }: { type: "reduce" | "offset" }) {
Step 2{" "}
</Text>
<Text> Step 1asdkjndsfkjcnkjsd </Text>
<Dropzone />
<Dropzone promptType={category} />

{/* Submit area */}
<Text fontSize="2xl" fontWeight="bold" paddingTop="2">
Expand Down

0 comments on commit 12a43f7

Please sign in to comment.