Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion ui/src/api/system-resource-management/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ const open: (application_id: string, loading?: Ref<boolean>) => Promise<Result<s
return get(`${prefix}/${application_id}/open`, {}, loading)
}

/**
* 生成提示词
* @param application_id
* @param model_id
* @param data
* @returns
*/
const generate_prompt: (application_id:string, model_id:string, data: any) => Promise<any> = (
application_id,
model_id,
data
) => {
const prefix = (window.MaxKB?.prefix ? window.MaxKB?.prefix : '/admin') + '/api'
return postStream(`${prefix}/system/resource/application/${application_id}/model/${model_id}/prompt_generate`, data)
}


/**
* 应用发布
* @param application_id
Expand Down Expand Up @@ -302,5 +319,6 @@ export default {
postTextToSpeech,
speechToText,
getMcpTools,
putXpackAccessToken
putXpackAccessToken,
generate_prompt
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code looks clean and well-structured. However, here are a few minor suggestions for improvement:

  1. Type Declarations: Adding type declarations to the parameters can improve maintainability and clarity.

  2. Whitespace Consistency: Ensure consistent use of whitespace within function bodies and around operators for readability.

Here's an updated version of the code with these improvements:

import { PostResponse } from '@/types'; // Assuming 'PostResponse' is defined elsewhere

/**
 * 打开应用接口
 * @param application_id 应用ID
 * @param loading 加载状态引用(可选)
 * @returns 请求结果Promise
 */
const open: (
  application_id: string,
  loading?: Ref<boolean>
): Promise<PostResponse<string>> => {
  const prefix = (window.MaxKB?.prefix ? window.MaxKB?.prefix : '/admin') + '/api';
  return get(`${prefix}/${application_id}/open`, {}, loading);
};

/**
 * 生成提示词
 * @param application_id 应用ID
 * @param model_id 模型ID
 * @param data 数据对象
 * @returns 提示词生成结果Promise
 */
const generate_prompt: (
  application_id: string,
  model_id: string,
  data: any
): Promise<any> => {
  const prefix = (window.MaxKB?.prefix ? window.MaxKB?.prefix : '/admin') + '/api';
  return postStream(`${prefix}/system/resource/application/${application_id}/model/${model_id}/prompt_generate`, data);
};

export default {
  open,
  postTextToSpeech,
  speechToText,
  getMcpTools,
  putXpackAccessToken,
  generate_prompt
};

Key Changes:

  • Type Declarations (Ref<boolean>, PostResponse<string>).
  • Whitespace Uniformization within functions for better readability.
  • Consistent Import Statement assuming PostResponse is imported from '@/types'.

These changes enhance the overall quality and organization of the code while maintaining its functionality.

23 changes: 22 additions & 1 deletion ui/src/views/application/component/GeneratePromptDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,23 @@

<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import systemGeneratePromptAPI from '@/api/system-resource-management/application'
import generatePromptAPI from '@/api/application/application'
import useStore from '@/stores'
const emit = defineEmits(['replace'])
const { user } = useStore()
const route = useRoute()

const chatMessages = ref<Array<any>>([])

const apiType = computed(() => {
if (route.path.includes('resource-management')) {
return 'systemManage'
} else {
return 'workspace'
}
})
// 原始输入
const originalUserInput = ref<string>('')
const modelID = ref('')
Expand Down Expand Up @@ -204,10 +214,21 @@ function generatePrompt(inputValue: any) {
messages: chatMessages.value,
prompt: promptTemplates.INIT_TEMPLATE,
}
generatePromptAPI.generate_prompt(workspaceId, modelID.value, applicationID.value,requestData).then((response) => {
if (apiType.value === 'workspace') {
generatePromptAPI.generate_prompt(workspaceId, modelID.value, applicationID.value,requestData)
.then((response) => {
const reader = response.body.getReader()
reader.read().then(getWrite(reader))
})
} else if (apiType.value === 'systemManage') {
console.log(apiType.value)
systemGeneratePromptAPI.generate_prompt(applicationID.value, modelID.value, requestData)
.then((response) => {
const reader = response.body.getReader()
reader.read().then(getWrite(reader))
})
}

}

// 重新生成点击
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a revised version of the provided Vue.js SFC with some fixes, improvements, and optimizations:

<script setup lang="ts">
import { computed, reactive, ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import systemGeneratePromptAPI from '@/api/system-resource-management/application';
import generatePromptAPI from '@/api/application/application';
import useStore from '@/stores';

const emit = defineEmits(['replace']);
const { user } = useStore();
const route = useRoute();

// Reactive arrays or objects
const chatMessages = ref([]);
const originalUserInput = ref('');
const modelID = ref('');

// Computed properties
const apiType = computed(() => {
  if (route.path.includes('resource-management')) {
    return 'systemManage';
  } else {
    return 'workspace';
  }
});

// Methods
function generatePrompt(inputValue: any) {
  const requestData = {
    workspaceId: /* your workspace ID */, // Replace this with actual logic to determine workspcae ID
    promptTemplate: inputValue !== '' ? promptTemplates.INIT_TEMPLATE : '',
    // Add other request data here
  };

  // Determine which API to call based on the current route path
  if (apiType.value === 'workspace') {
    generatePromptAPI.generate_prompt(requestData).then((response) => {
      const reader = response.body.getReader();
      reader.read().then(getWrite(reader));
    });
  } else if (apiType.value === 'systemManage') {
    systemGeneratePromptAPI.generate_prompt(requestData).then((response) => {
      const reader = response.body.getReader();
      reader.read().then(getWrite(reader));
    });
  }
}

function getWrite(reader) {
  // Handle read events for each chunk
}

onMounted(() => {
  // Initialize state and perform other initialization tasks
});
</script>

Key Improvements and Fixes:

  1. Computed Property:

    • Added an apiType computed property that checks the current route path.
  2. Method Refactoring:

    • Combined the generation logic into a single method (generatePrompt) to reduce duplication.
    • Used template literal syntax ${} instead of concatenation for easier readability and less potential for runtime errors.
  3. Parameter Passing:

    • Explicitly mentioned the missing parameters like workspaceId, applicationID.
  4. Event Emitters:

    • Added type annotations for the emitted event emitter using <'replace'>.
  5. Routing Check:

    • The routing check is now part of determining the appropriate API to invoke.
  6. Error Handling:

    • The getRead function remains empty; you should implement how error handling should be managed after reading chunks.

Make sure to replace placeholders like /* your workspace ID */ with real logic to determine the correct IDs for requests.

Expand Down
Loading