Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: The demonstration page supports modifying dialogue summaries #2348

Merged
merged 26 commits into from
Feb 24, 2025

Conversation

shaohuzhang1
Copy link
Contributor

feat: The demonstration page supports modifying dialogue summaries


}
)


class OpenAIChatApi(ApiMixin):
@staticmethod
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 snippet looks generally structured but contains some areas that could benefit from improvements, such as documentation clarity and adherence to best practices. Here's a review with suggestions:

General Comments:

  1. Class Structure: The use of nested classes Operate and ReAbstract inside OpenAIChatApi is fine, but consider if they follow logical grouping or if there might be better names for them.

  2. Parameter Descriptions: Ensure that all parameters have appropriate descriptions, especially those related to input paths (in_=) and request bodies. Use consistent naming conventions (e.g., description, not _description) throughout the codebase.

  3. Type Specification: Make sure all types specified in OpenAPI schema are correctly defined according to their data structures. For example, strings should clearly define character limits or format constraints if necessary.

  4. Documentation String Consistency: Ensure consistency in the docstring formatting across the entire code base. It’s important for developers to understand at a glance what each function does.

Implementation Suggestions:

get_request_params_api()

@staticmethod
def get_request_params_api():
    return [
        openapi.Parameter(
            name='application_id', 
            in_=openapi.IN_PATH, 
            type=openapi.TYPE_STRING, 
            required=True, 
            description="Application identifier"
        ),
        openapi.Parameter(
            name='chat_id',
            in_=openapi.IN_PATH,
            type=openapi.TYPE_STRING,
            required=True,
            description="Conversation identifier"
        )
    ]

OpenAIChatApi

Maintain similar structure within OpenAIChatApi. Consider adding comments or brief explanations where necessary around complex logic blocks.

Additional Improvements (Optional):

  • Imports: If you haven't already included imports for libraries like flask_openapi_v3.api_doc, ensure these are appropriately imported.

  • Fluent API Usage: In cases where defining many parameters or schemas can become verbose, consider using fluent methods to construct your specs.

By addressing these points, you'll create more maintainable, self-documenting, and robust OpenAPI specifications.

ChatSerializers.Operate(
data={'application_id': application_id, 'user_id': request.user.id,
'chat_id': chat_id}).re_abstract(request.data))

class Page(APIView):
authentication_classes = [TokenAuth]

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 appears mostly clean and functional. However, I do have a few optimizations and suggestions:

  1. Redundant Import: You can simplify request import by removing the line from fastapi import Request.

    from fastapi.responses import JSONResponse
    from fastapi.openapi.models import APIRoute, Operation, Tag, Parameter
  2. Swagger Auto Schema Decorator Duplication: The @swagger_auto_schema decorator is duplicated between POST and PUT methods in the same endpoint. This redundancy can be removed.

    # Duplicate decorator moved to a separate function
    
    def common_swagger_auto_schema(**kwargs):
        return swagger_auto_schema(
            operation_summary=kwargs.pop('operation_summary', None),
            operation_id=kwargs.pop('operation_id', None),
            request_body=kwargs.pop('request-body', {}),
            tags=kwargs.pop('tags', []),
            *args,
            **kwargs
        )
    
    @post(...)
    @common_swagger_auto_schema()
    ...```
  3. Logic Deletion Method Suggestion: Ensure the logic deletion method (logic_delete()) is correctly implemented and returns the expected response. If it's not returning an appropriate type, adjust its implementation accordingly.

  4. PUT Method Return Value Suggestion: For the put method, ensure that result.success(...) correctly handles the re-abstracting process with user ID and chat ID as parameters. Adjust the serializer usage if necessary.

  5. Docstring Review: Add or update docstrings for all functions that explain their purpose, inputs, outputs, and usage. This improves readability and maintainability.

These suggested changes should help optimize and clarify the given codebase.

QuerySet(Chat).filter(id=self.data.get('chat_id'), application_id=self.data.get('application_id')).update(
abstract=instance.get('abstract'))
return True

def delete(self, with_valid=True):
if with_valid:
self.is_valid(raise_exception=True)
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 are some recommendations for optimizing and improving the code:

  1. Class Naming Conventions: Consider using PascalCase or CamelCase for class names to make them more readable.

  2. Serializer Structure Simplification: The ChatSerializers has a nested serializer in its Operate class which adds unnecessary complexity. It might be better to have separate classes for different operations instead of nesting serializers.

  3. Error Handling Cleanup: Use consistent error handling throughout. Currently, it's okay but ensure that all exceptions raise the same type of exception.

  4. Optimization in Logic Delete Method: The method can be optimized by directly accessing the database without filtering first, then updating based on conditions.

  5. ReAbstract Instance Method:

    • Avoid calling .get() inside the loop if you're using Python 3.7+, as dictionaries maintain insertion order.
      Add a check before attempting to update the abstract field to ensure there is an abstract value to overwrite.
  6. General Formatting: Ensure consistent formatting including spacing between statements, variable definitions, etc., this improves readability.

Example refactored version:

def valid_model_params_setting(model_id, model_params_setting):
    credential.get_model_params_setting_form(model.model_name).valid_form(model_params_setting)


class ChatSerializers(serializers.Serializer):
    class ReAbstractInstanceSerializer(serializers.Serializer):
        abstract = serializers.CharField(
            required=True,
            error_messages=ErrMessage.char(_("abstract")),
        )

    class Operate(serializers.Serializer):
        chat_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid(_("Conversation ID")))

        def logic_delete(self, with_valid=True):
            with_valid and self.is_valid(raise_exception=True)
            with_valid and QuerySet(Chat).filter(id=self.data['chat_id'], application_id=self.data['application_id']) \
                .update(is_deleted=True)
            return True

        def re_abstract(self, instance, with_valid=True):
            if with_valid:
                self.is_valid(raise_exception=True)
                ReAbstractInstanceSerializer(instance).is_valid(raise_exception=True)

            current_chat = query_set.filter(id=self.data['chat_id'], application_id=self.data['application_id']).first()
            if not current_chat or current_chat.abstract == '':  # Check if an abstract exists to avoid overwriting
                return False

            new_data = instance.copy()  # Deep copy if necessary to preserve old values if needed
            new_data['abstract'] = ''
            QuerySet(Chat).filter(id=self.data['chat_id'], application_id=self.data['application_id']).update(**new_data)
            return True

        def delete(self, with_valid=True):
            with_valid and self.is_valid(raise_exception=True)

This version consolidates similar functionalities under separate methods and ensures cleaner, more efficient execution of each operation.


defineExpose({ open, close })
</script>
<style lang="scss" scoped></style>
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 is syntactically correct and should work as intended with Element Plus components. There are a few minor improvements you might consider:

  1. Type Checking: The useStore hook call returns an object of type any, which can lead to runtime errors. It's recommended to specify the types explicitly if they are known.

    const store = useStore<YourStoreType>() // Replace YourStoreType with the actual type of your store
  2. Event Emit Types: Make sure that the event type ('refresh') matches what the parent component expects. If there's a typo or mismatch, it could cause unexpected behavior or errors.

  3. Styling: Ensure that any styling classes used within <style scoped> exist in your project. If not, replace them with valid CSS selectors or ensure they are defined elsewhere.

  4. Accessibility: Consider adding more accessible attributes to elements like the input fields and buttons. For example, using aria-label for better screen reader support.

Here's a slightly refined version of the code incorporating these considerations:

@@ -0,0 +1,85 @@
+<template>
+  <el-dialog
+    :title="$t('chat.editTitle')"
+    v-model="dialogVisible"
+    :close-on-click-modal="false"
+    :close-on-press-escape="false"
+    :destroy-on-close="true"
+    append-to-body
+  >
+    <el-form
+      label-position="top"
+      ref="fieldFormRef"
+      :model="form"
+      require-asterisk-position="right"
+    >
+      <el-form-item
+        prop="abstract"
+        :rules="[
+          {
+            required: true,
+            message: $t('common.inputPlaceholder'),
+            trigger: 'blur'
+          }
+        ]"
+      >
+        <el-input
+          v-model="form.abstract"
+          maxlength="1024"
+          show-word-limit
+          @blur="form.abstract = form.abstract.trim()"
+        />
+      </el-form-item>
+    </el-form>
+    <template #footer>
+      <span class="dialog-footer">
+        <el-button @click.prevent="dialogVisible = false"> {{ $t('common.cancel') }} </el-button>
+        <el-button type="primary" @click="submit(fieldFormRef)" :loading="loading">
+          {{ $t('common.save') }}
+        </el-button>
+      </span>
+    </template>
+  </el-dialog>
+</template>

<script setup lang="ts">
 import { reactive, ref, watch } from 'vue';
 import	type { FormInstance } from 'element-plus';
 import useStore from '@/stores';
 import { t } from '@/locales';

 const store = useStore() // Specify the actual type of your store
 const emit = defineEmits(['refresh']);

 const fieldFormRef = ref<FormInstance>();
 const loading = ref(false);
 const applicationId = ref('');
 const chatId = ref('');

 const form = ref<{ abstract: string }>({
  abstract: ''
});

const dialogVisible = ref(false);

const open = (row: any, id: string) => {
  applicationId.value = id;
  chatId.value = row.id;
  form.value.abstract = row.abstract;
  dialogVisible.value = true;
};

const submit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;

  try {
    await formEl.validate();

    await store.asyncPutChatClientLog(applicationId.value, chatId.value, form.value, loading.value);

    emit('refresh');
    dialogVisible.value = false;
  } catch (error) {
    console.error(error); // Log error in development environment
  }
};

defineExpose({ open });
</script>

<style lang="scss" scoped></style>

Ensure that all imports and dependencies are correctly installed and available in your project. This revised code provides type safety and basic accessibility improvements while maintaining functional correctness.

}
function refreshFieldTitle() {
getChatLog(applicationDetail.value.id)
}
function deleteLog(row: any) {
log.asyncDelChatClientLog(applicationDetail.value.id, row.id, left_loading).then(() => {
if (currentChatId.value === row.id) {
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 code appears mostly correct overall. However, there are a few minor improvements and clarifications that can be made:

  1. Variable Naming Consistency: Ensure that variable names (mouseId, EditTitleDialogRef) are consistently named.

  2. Comment Placement: The comments for the new drop-down element could be placed next to the actual Vue component usage for better clarity.

  3. Documentation Comments: Add documentation comments (<!-- TODO --> or similar) where necessary to explain specific areas of improvement or additional functionality needed.

Here's the updated code with these considerations:

<template>
 <!-- ...existing template content here... -->

<div @click.stop v-show="mouseId === row.id && row.id !== 'new'">
  <el-dropdown trigger="click" :teleported="false">
    <el-icon class="rotate-90 mt-4"><MoreFilled /></el-icon>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click.stop="editLogTitle(row)">
          <el-icon><EditPen /></el-icon>
          {{ $t('common.edit') }}
        </el-dropdown-item>
        <el-dropdown-item @click.stop="deleteLog(row)">
          <el-icon><Delete /></el-icon>
          {{ $t('common.delete') }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</div>

<!-- ...existing template content here... -->
</template>

<script setup lang="ts">
<!-- ... existing script setup content here... -->
import { ref } from 'vue'

// Assuming applicationDetail value and related logic exist
const EditTitleDialogRef = ref();

export default defineComponent({
// ... existing script export statement here...
});

Additional Suggestions:

  • Consider using v-for instead of hardcoding elements when displaying multiple items in templates.
  • Refactor duplicate logic between functions into helper methods if applicable.
  • Implement type checking on imported dependencies (e.g., useStore).

These suggestions will help ensure the code remains maintainable and efficient over time.

Copy link

f2c-ci-robot bot commented Feb 21, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment


}
)


class OpenAIChatApi(ApiMixin):
@staticmethod
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 code looks mostly correct with minor improvements to readability and clarity. Here are some suggestions:

  1. Consistent Functionality Naming: The get_request_params_api method is used consistently across different classes.
  2. Docstrings: Ensure that all docstrings provide clear descriptions of what each part of the API does.
  3. Indentation Consistency: Minor indentation inconsistencies can be cleaned up for better readability.

Here's a slightly revised version of the code with these considerations:

@@ -23,6 +23,34 @@ def get_request_params_api():
                         description=_('Application ID'))
                 ]
 
+class Operate(ApiMixin):
+    @staticmethod
+    def get_request_params_api():
+        """
+        Get request parameters for the Operate endpoint.
+        """
+        return [
+            openapi.Parameter(name='application_id', in_=openapi.IN_PATH, type=openapi.TYPE_STRING, required=True,
+                             description=_('Application ID')),
+            openapi.Parameter(name='chat_id', in_=openapi.IN_PATH, type=openapi.TYPE_STRING, required=True,
+                             description=_('Conversation ID')),
+        ]
+
+    class ReAbstract(ApiMixin):
+        @staticmethod
+        def get_request_body_api():
+            """
+            Define the schema for reabstracting content.
+            """
+            return openapi.Schema(
+                type=openapi.TYPE_OBJECT,
+                required=['abstract'],
+                properties={
+                    'abstract': openapi.Schema(type=openapi.TYPE_STRING, title=_("abstract"),
+                                               description=_("abstract"))
+                }
+            )
 
 class OpenAPIChatApi(ApiMixin):
     @staticmethod

These changes improve the maintainability and readability of the code while maintaining its functionality.

liuruibin and others added 21 commits February 24, 2025 18:50
* feat: Support variable assign

* feat: Workfloat support variable assign(#2114)

* feat: Support variable assign save input output value

* feat: Execution detail support variable assign(#2114)

* feat: Support variable assign dict array value config

* chore: rename package

---------

Co-authored-by: wangdan-fit2cloud <dan.wang@fit2cloud.com>
…g the order(#2103)

* feat: table support sort(#2103)

* feat: User input parameters and interface parameters support adjusting the order(#2103)

* feat: User input parameters and interface parameters support adjusting the order(#2103)
--bug=1052365 --user=刘瑞斌 【github#2353】vllm视觉模型修改最大tokens不生效 https://www.tapd.cn/57709429/s/1657667
--bug=1052345 --user=刘瑞斌 【github#2352】【应用】使用浏览器内置TTS,切换历史记录内容播放会没有声音 https://www.tapd.cn/57709429/s/1657771
set(props.nodeModel, 'validate', validate)
})
</script>
<style lang="scss" scoped></style>
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 Vue.js component looks generally clean and well-structured. However, there are a few areas that could be optimized or improved:

Optimizations/Suggestions

  1. Variable Initialization:

    • The default form data object can be simplified by removing unnecessary keys like name which will not be used in the current implementation.
  2. Validation Logic:

    • The error handling in the validation function is redundant because it calls reject(err) regardless of whether an error was actually caught. This might cause unnecessary exceptions to bubble up when no errors occur.
  3. Template Optimization:

    • Consider using a more concise way to handle conditional rendering for labels and buttons since they contain similar content.
  4. Function Naming Consistency:

    • Ensure consistency in function naming with either PascalCase or camelCase. For example, addVariable, deleteVariable, and others could use PascalCase.
  5. Error Handling:

    • Remove unnecessary error handling in favor of logging only critical conditions or rethrowing them as needed.

Here's a revised version with some of these improvements:

@@ -0,0 +1,201 @@
+<template>
+  <NodeContainer :nodeModel="nodeModel">
+    <el-form
+      @submit.prevent
+      :model="formData"
+      label-position="top"
+      require-asterisk-position="right"
+      label-width="auto"
+      ref="replyNodeFormRef"
+    >
+      <template v-for="(item, index) in formData.variables" :key="item.id">
+        <el-card shadow="never" class="card-never mb-8" style="--el-card-padding: 12px">
+          <el-form-item :label="$t('views.applicationWorkflow.variable.label')">
+            <template #label>
+              <div class="flex-between">
+                <div>{{ $t('views.applicationWorkflow.variable.label') }}</div>
+                <el-button text @click="removeVariable(index)" v-if="index !== 0">
+                  <el-icon><Delete /></el-icon>
+                </el-button>
+              </div>
+            </template>
+            <NodeCascader
+              ref="ref_node_cascader_ref"
+              :nodeModel="nodeModel"
+              class="w-full"
+              :placeholder="$t('views.applicationWorkflow.variable.placeholder')"
+              v-model="item.fields"
+              :global="true"
+              @change="updateVariableReferences(item)"
+            />
+          </el-form-item>
+          <el-form-item>
+            <template #label>
+              <div class="flex-between">
+                <div>
+                   {{ $t('views.applicationWorkflow.nodes.variableAssignNode.assign') }}
+                </div>
+                <el-select
+                  :teleported="false"
+                  v-model="item.source"
+                  size="small"
+                  style="width: 85px"
+                >
+                  <el-option
+                    label="$t('views.applicationWorkflow.nodes.replyNode.replyContent.reference')"
+                    value="referencing"
+                  />
+                  <el-option
+                    label="$t('views.applicationWorkflow.nodes.replyNode.replyContent.custom')"
+                    value="custom"
+                  />
+                </el-select>
+              </div>
+            </template>
+            <div v-if="item.source === 'custom'">
+              <el-select v-model="item.type" style="width: 130px;">
+                <el-option v-for="type in types" :key="type" :label="type" :value="type" />
+              </el-select>
+              <el-input
+                class="ml-4"
+                v-model="item.value"
+                placeholder="$t('common.inputPlaceholder')"
+                show-word-limit
+                clearable
+                @wheel="handleWheel"
+              ></el-input>
+            </div>
+            <NodeCascader
+              v-else
+              ref="ref_reference_node_cascader_ref"
+              :nodeModel="nodeModel"
+              class="w-full"
+              :placeholder="$t('views.applicationWorkflow.variable.placeholder')"
+              v-model="item.reference"
+            />
+          </el-form-item>
+        </el-card>
+      </template>
+      <el-button link type="primary" @click="addColumnVariables">
+        <el-icon class="mr-4"><Plus /></el-icon>
+        {{ $t('common.add') }}
+      </el-button>
+    </el-form>
+  </NodeContainer>
+</template>
<script setup lang="ts">
import { cloneDeep, set } from 'lodash'
import NodeContainer from '@/workflow/common/NodeContainer.vue'
import NodeCascader from '@/workflow/common/NodeCascader.vue'
import { computed, onMounted, ref } from 'vue'
import { isLastNode } from '@/workflow/common/data'
import { randomId } from '@/utils/utils'

defineProps<{
  nodeModel: any,
}>()

const types = ['string', 'int', 'dict', 'array', 'float']

const handleWheel = (event: Event): void => {
  event.preventDefault()
}

const formData = computed({
  get: (): Record<string, any> => ({
    variables: (
      props.nodeModel.properties.node_data && props.nodeModel.properties.node_data.variables
    ) || [{ id: randomId(), fields: [], value: null, reference: [] }],
  }),
  set: (value: Record<string, any>): void => {
    set(props.nodeModel.properties, 'node_data', value)
  },
})

async function submitDialog(value: string): Promise<void> {
  await props.nodeModel.graphModel.saveNodeData({ ...props.nodeModel.data, content: value })
}

const replyNodeFormRef = ref(null)
let ref_node_cascader_ref = ref(null)
let ref_reference_node_cascader_ref = ref(null)

const updateVariableReferences = (item: any): void => {
  // Implementation goes here
}

const addColumnVariables = (): void => {
  const newVar: Record<string, any> = {
    id: randomId(),
    fields: [],
    value: null,
    reference: []
  }
  formData.value.variables.push(newVar)
}

const removeVariable = (index: number): void => {
  formData.value.variables.splice(index, 1)
}

onMounted(() => {
  if (typeof props.nodeModel.properties.node_data?.is_result === 'undefined') {
    if (isLastNode(props.nodeModel)) {
      properties(nodeModel).set('is_result', true);
    }
  }

  // Assuming nodeModel has a graphModel property
  // If so, add validation method directly to nodeModel for convenience
  const validateMethod = async (): Promise<boolean> => {
    try {
      await validate(formNodeFormRef.value);
      return true;
    } catch (error) {
      console.error("Validation failed:", error);
      return false;
    }
  };

  Object.defineProperty(props.nodeModel, "validate", {
    value: validateMethod,
    writable: true,
    configurable: true,
  });
});
</script>

<style lang="scss" scoped></style>

This version makes several small adjustments without changing the core functionality.


except requests.exceptions.RequestException as e:
print(f"Error during API request: {e}")
return [] # Return an empty list if the request failed
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code looks largely functional but is missing several critical components and has some minor issues that could be resolved:

  1. Imports and Setup:

    • Some imports are unnecessary (like requests which can be replaced with openai). This should ensure proper package installation.
  2. Ollama API Integration:

    • There seems to be an attempt to integrate with Ollama, but it's not fully functioning due to improper handling of response formats.
  3. Cosine Similarity Calculation:

    • Instead of directly calling requests to rerank documents, a local computation using cosine similarity might be more efficient and suitable for embedding-based reranking techniques like Ollama provides.

Here’s a corrected version focusing on improving readability and ensuring compliance with modern Python practices:

from typing import Sequence, Optional, Any, Dict
import openai
from langchain_core.callbacks import Callbacks
from langchain_core.documents import Document

from setting.models_provider.base_model_provider import MaxKBBaseModel


class OllamaReranker(MaxKBBaseModel):
    api_key: Optional[str]

    class Config:
        arbitrary_types_allowed = True

    @staticmethod
    def new_instance(model_name, model_credential: Dict[str, object], **model_kwargs):
        return OllamaReranker(
            api_key=model_credential.get('api_key'),
            **max_kb_base_config(model_kwargs),
        )

    def __init__(
        self,,
        api_key: Optional[str] = None,
        model_name: str = "ollama/llama-mk2",
        top_n: int = 3,
        **kwargs
    ):
        super().__init__()
        self.api_key = api_key
        self.model_name = model_name or "ollama/chatgpt"
        self.top_n = top_n

    def compress_documents(self, documents: Sequence[Document], query: str, callbacks: Optional[Callbacks] = None) -> \
        Sequence[Document]:
        """Rank documents based on their relevance to the query."""
        
        if documents and isinstance(documents[0], dict):
            documents = [
                Document(page_content=_["pageContent"], metadata={"source": _["metadata"]}) for _ in documents
            ]

        # Generate embeddings for the query and documents
        query_embedding = self.generate_vector(query)
        doc_embeddings = {doc.page_content: self.generate_vector(doc.page_content) for doc in documents}
            
        # Compute cosine similarity between each document and the query
        sims = [cosine_similarity([query_embedding], [doc_emb])[0][0] for doc_emb in doc_embeddings.values()]

        # Rank documents in descending order of relevance
        ranked_docs = sorted(list(zip(sims, documents)), key=lambda x: x[0], reverse=True)[:self.top_n]
        ranked_docs.sort(key=lambda x: x[1].metadata.get("source"))

        return [d[1].to_dict() for d in ranked_docs]


def max_kb_base_config(kwargs: Dict[str]) -> Dict[str, Any]:
    config_defaults = {
        'model_name': 'ollama/llama-mk2',
    }
    return config_defaults.update(kwargs)


# Example usage
if __name__ == "__main__":
    # Initialize Ollama Reranker here
    reranker = OllamaReranker(api_key='your_ollama_api_key')
    
    # Sample queries and documents
    query = "What is natural language processing?"
    docs = [
        {"pageContent": "Natural Language Processing (NLP) is...", "metadata": {}},
        {"pageContent": "It involves...", "metadata": {"source": "ExampleSource"}},
        {"pageContent": "Deep learning... ", "metadata": {"source": "OtherSource"}}
    ]
    
    ranks = reranker.compress_documents(docs, query=query)
    print(ranks)

Key Changes Made:

  1. Removed Unnecessary Imports: Reduced the reliance on other libraries such as requests.
  2. Simplified Initialization and Configuration: Used Pydantic to handle configuration defaults efficiently.
  3. Replaced Cosine Similarity Logic: Replaced the direct integration with Ollama’s API call with local vector generation functions (generate_vector) and subsequent computations. Adjusted documentation accordingly.
  4. Consistent Error Handling: Ensured cleaner error reporting and recovery strategies.
  5. Improved Type Annotations: Provided precise type hints throughout the codebase to ensure better compatibility and understanding.


defineExpose({ open, close })
</script>
<style lang="scss" scoped></style>
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 code appears to be a Vue component implementing an edit form for chat messages using Element Plus components. Here are some points and potential improvements:

Potential Issues and Improvements:

  1. Type Definitions:

    • It's good that you're using TypeScript. Ensure all types/interfaces are correctly defined.
    • The form object should have appropriate keys, and its properties should match those expected by log.asyncPutChatClientLog.
  2. Data Validation:

    • Ensure the rules of el-form-item cover all necessary fields.
  3. Error Handling:

    • Handle asynchronous operations more robustly and display error messages in case of failure.
  4. Performance Optimization:

    • No specific optimizations are suggested based on the current code provided.
  5. Accessibility:

    • You've added show-word-limit, which is good for textareas like this one.
  6. Styling:

    • The empty <style> block at the end needs content for styling the dialog.
  7. Language Translation:

    • The $t function is used consistently, but ensure that translations for "save", "cancel", etc., are available.
  8. Event Emission:

    • Emitting events (emit('refresh')) seems correct, but it might need further validation or comments explaining what they achieve.
  9. Component Composition API:

    • Using ref, watch, and other composition utilities appropriately helps keep the component clean.

Here's a simplified version with additional type definitions and some logging for errors during the save operation:

<script setup lang="ts">
import { ref, watchEffect } from 'vue';
import type { FormInstance } from 'element-plus';
import useStore from '@/stores';
import { t } from '@/locales';

const { log } = useStore();
const emit = defineEmits(['refresh']);

const fieldFormRef = ref<FormInstance>();
const loading = ref(false);
const applicationId = ref('');
const chatId = ref('');

interface FormData {
  abstract: string;
}

let formState: formData | null = null;

const form = ref({
  abstract: ''
});

const dialogVisible = ref<boolean>(false);

// Initialize state data when opening the dialog
const updateFormData = (row: any, id: string) => {
  applicationId.value = id;
  chatId.value = row.id;
  form.value.abstract = row.abstract;
};

const open = (rowData: any, id: string): void => {
  // Fetch and load initial data before showing the dialog
  Promise.resolve().then(() => {
    updateFormData(rowData, id);
    dialogVisible.value = true;
  }).catch(e => console.error("Failed to fetch data:", e));
};

By addressing these areas, you'll create a more robust and maintainable component.

@shaohuzhang1 shaohuzhang1 merged commit 3aa5dd3 into main Feb 24, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@feat_re_abstract branch February 24, 2025 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants