-
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.
Merge branch 'feature/f-33-chat-implementation' of https://github.com…
…/jst-seminar-rostlab-tum/wfp-hunger-map into feature/f-33-chat-implementation
- Loading branch information
Showing
6 changed files
with
96 additions
and
90 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
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
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,26 @@ | ||
import { IMessage } from './Chatbot'; | ||
|
||
export interface QueryRequest { | ||
query: string; | ||
version?: number; | ||
chatbot_type?: string; | ||
limit?: number; | ||
previous_messages?: IMessage[]; | ||
} | ||
|
||
export interface QueryResponse { | ||
response: string; | ||
prompt_tokens: number; | ||
total_tokens: number; | ||
} | ||
|
||
// TO-DO: not sure if it belongs here. consider moving to another place | ||
export class APIError extends Error { | ||
constructor( | ||
public status: number, | ||
message: string | ||
) { | ||
super(message); | ||
this.name = 'APIError'; | ||
} | ||
} |
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,17 @@ | ||
import { QueryRequest, QueryResponse } from '@/domain/entities/chatbot/BackendCommunication.ts'; | ||
|
||
export default interface ChatbotRepository { | ||
/** | ||
* Tests the health of the chatbot service by sending a test message. | ||
* @returns A promise that resolves to a boolean indicating the health status. | ||
*/ | ||
testHealth(): Promise<boolean>; | ||
|
||
/** | ||
* Sends a message to the chatbot service with optional parameters. | ||
* @param message The message to be sent to the chatbot. | ||
* @param options Optional parameters for the message, excluding the query. | ||
* @returns A promise that resolves to the response from the chatbot. | ||
*/ | ||
sendMessage(message: string, options: Partial<Omit<QueryRequest, 'query'>>): Promise<QueryResponse>; | ||
} |
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,45 @@ | ||
import { APIError, QueryRequest, QueryResponse } from '@/domain/entities/chatbot/BackendCommunication.ts'; | ||
import ChatbotRepository from '@/domain/repositories/ChatbotRepository'; | ||
|
||
export default class ChatbotRepositoryImpl implements ChatbotRepository { | ||
async testHealth(): Promise<boolean> { | ||
try { | ||
const response = await fetch(`${process.env.BACKEND_CHATBOT_API_URL}/health`); | ||
return response.ok; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
async sendMessage(message: string, options: Partial<Omit<QueryRequest, 'query'>>): Promise<QueryResponse> { | ||
try { | ||
const payload: QueryRequest = { | ||
query: message, | ||
version: options.version || 1, | ||
chatbot_type: options.chatbot_type || 'gpt-4', | ||
limit: options.limit || 5, | ||
previous_messages: options.previous_messages || [], | ||
}; | ||
|
||
const response = await fetch(`${process.env.BACKEND_CHATBOT_API_URL}/query`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(payload), | ||
}); | ||
|
||
if (!response.ok) { | ||
const errorData = await response.json().catch(() => ({})); | ||
throw new APIError(response.status, errorData.error || `Server responded with status: ${response.status}`); | ||
} | ||
const data = await response.json(); | ||
return data; | ||
} catch (error) { | ||
if (error instanceof APIError) { | ||
throw error; | ||
} | ||
throw new APIError(500, 'Failed to connect to server'); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.