ai-api.service.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Injectable } from '@angular/core';
  2. import { TestCompletion, generateInterviewEvaluation } from './completion';
  3. export interface TestMessage {
  4. role: string;
  5. content: string;
  6. }
  7. @Injectable({
  8. providedIn: 'root'
  9. })
  10. export class AiApiService {
  11. private positionRequirements = `
  12. 岗位要求:
  13. - 3年以上相关工作经验
  14. - 精通至少一种主流编程语言
  15. - 良好的沟通能力和团队协作精神
  16. - 解决问题的能力
  17. `;
  18. constructor() { }
  19. async evaluateInterviewAnswer(
  20. question: string,
  21. answer: string,
  22. onMessage?: (content: string) => void
  23. ) {
  24. try {
  25. const evaluation = await generateInterviewEvaluation(
  26. question,
  27. answer,
  28. this.positionRequirements,
  29. onMessage
  30. );
  31. // 确保返回标准化的评估结果
  32. return {
  33. score: evaluation.score || 0,
  34. feedback: evaluation.feedback || "暂无反馈",
  35. followUpQuestion: evaluation.followUpQuestion || "能否详细说明一下?",
  36. metrics: {
  37. expressiveness: evaluation.metrics?.expressiveness || 0,
  38. professionalism: evaluation.metrics?.professionalism || 0,
  39. relevance: evaluation.metrics?.relevance || 0
  40. }
  41. };
  42. } catch (error) {
  43. console.error('评估失败:', error);
  44. return this.getDefaultEvaluation();
  45. }
  46. }
  47. private getDefaultEvaluation() {
  48. return {
  49. score: 70,
  50. feedback: "回答基本符合要求,但缺乏具体细节",
  51. followUpQuestion: "能否详细说明一下?",
  52. metrics: {
  53. expressiveness: 70,
  54. professionalism: 65,
  55. relevance: 60
  56. }
  57. };
  58. }
  59. async generateFollowUpQuestion(
  60. conversationHistory: TestMessage[],
  61. onMessage?: (content: string) => void
  62. ): Promise<string> {
  63. const prompt = `根据以下对话历史,生成一个有针对性的追问问题:
  64. ${conversationHistory.map(msg => `${msg.role}: ${msg.content}`).join('\n')}
  65. 要求:
  66. - 问题应针对候选人的回答深入挖掘
  67. - 问题应简洁明了
  68. - 只返回问题内容,不要包含其他说明`;
  69. const messages: TestMessage[] = [
  70. { role: "user", content: prompt }
  71. ];
  72. const completion = new TestCompletion(messages);
  73. const result = await completion.sendMessage(messages, onMessage);
  74. return result || "能否详细说明一下?";
  75. }
  76. }