Skip to content

Commit

Permalink
fix: conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Jan 2, 2024
2 parents d6ee7bd + 6f65d12 commit f1c8364
Show file tree
Hide file tree
Showing 178 changed files with 5,977 additions and 4,772 deletions.
15 changes: 15 additions & 0 deletions api/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Celery",
"type": "python",
"request": "launch",
"module": "celery",
"justMyCode": true,
"args": ["-A", "app.celery", "worker", "-P", "gevent", "-c", "1", "--loglevel", "info", "-Q", "dataset,generation,mail"],
"envFile": "${workspaceFolder}/.env",
"env": {
"FLASK_APP": "app.py",
"FLASK_DEBUG": "1",
"GEVENT_SUPPORT": "True"
},
"console": "integratedTerminal"
},
{
"name": "Python: Flask",
"type": "python",
Expand Down
Empty file.
35 changes: 35 additions & 0 deletions api/core/external_data_tool/weather_search/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"label": {
"en-US": "Weather Search",
"zh-Hans": "天气查询"
},
"form_schema": [
{
"type": "select",
"label": {
"en-US": "Temperature Unit",
"zh-Hans": "温度单位"
},
"variable": "temperature_unit",
"required": true,
"options": [
{
"label": {
"en-US": "Fahrenheit",
"zh-Hans": "华氏度"
},
"value": "fahrenheit"
},
{
"label": {
"en-US": "Centigrade",
"zh-Hans": "摄氏度"
},
"value": "centigrade"
}
],
"default": "centigrade",
"placeholder": "Please select temperature unit"
}
]
}
45 changes: 45 additions & 0 deletions api/core/external_data_tool/weather_search/weather_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import Optional

from core.external_data_tool.base import ExternalDataTool


class WeatherSearch(ExternalDataTool):
"""
The name of custom type must be unique, keep the same with directory and file name.
"""
name: str = "weather_search"

@classmethod
def validate_config(cls, tenant_id: str, config: dict) -> None:
"""
schema.json validation. It will be called when user save the config.
Example:
.. code-block:: python
config = {
"temperature_unit": "centigrade"
}
:param tenant_id: the id of workspace
:param config: the variables of form config
:return:
"""

if not config.get('temperature_unit'):
raise ValueError('temperature unit is required')

def query(self, inputs: dict, query: Optional[str] = None) -> str:
"""
Query the external data tool.
:param inputs: user inputs
:param query: the query of chat app
:return: the tool query result
"""
city = inputs.get('city')
temperature_unit = self.config.get('temperature_unit')

if temperature_unit == 'fahrenheit':
return f'Weather in {city} is 32°F'
else:
return f'Weather in {city} is 0°C'
18 changes: 4 additions & 14 deletions api/core/model_runtime/model_providers/chatglm/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,11 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
:return:
"""
try:
response = post(join(credentials['api_base'], "v1/chat/completions"), data=dumps({
"model": model,
"messages": [
{
"role": "user",
"content": "ping"
}
],
}),
headers={
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"
self._invoke(model=model, credentials=credentials, prompt_messages=[
UserPromptMessage(content="ping"),
], model_parameters={
"max_tokens": 16,
})
if response.status_code != 200:
raise CredentialsValidateFailedError("Invalid credentials")
except Exception as e:
raise CredentialsValidateFailedError(str(e))

Expand Down
8 changes: 1 addition & 7 deletions api/core/model_runtime/model_providers/openllm/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ def get_customizable_model_schema(self, model: str, credentials: dict) -> AIMode
"""
used to define customizable model schema
"""
model_type = None
if credentials['model_type'] == 'text-generation':
model_type = ModelType.LLM
else:
raise Exception(f'Large Language Model {credentials["model_type"]} is not supported')

rules = [
ParameterRule(
name='temperature',
Expand Down Expand Up @@ -200,7 +194,7 @@ def get_customizable_model_schema(self, model: str, credentials: dict) -> AIMode
en_US=model
),
fetch_from=FetchFrom.CUSTOMIZABLE_MODEL,
model_type=model_type,
model_type=ModelType.LLM,
model_properties={
'mode': LLMMode.COMPLETION,
},
Expand Down
Empty file.
93 changes: 93 additions & 0 deletions api/core/moderation/cloud_service/cloud_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from core.moderation.base import Moderation, ModerationAction, ModerationInputsResult, ModerationOutputsResult


class CloudServiceModeration(Moderation):
"""
The name of custom type must be unique, keep the same with directory and file name.
"""
name: str = "cloud_service"

@classmethod
def validate_config(cls, tenant_id: str, config: dict) -> None:
"""
schema.json validation. It will be called when user save the config.
Example:
.. code-block:: python
config = {
"cloud_provider": "GoogleCloud",
"api_endpoint": "https://api.example.com",
"api_keys": "123456",
"inputs_config": {
"enabled": True,
"preset_response": "Your content violates our usage policy. Please revise and try again."
},
"outputs_config": {
"enabled": True,
"preset_response": "Your content violates our usage policy. Please revise and try again."
}
}
:param tenant_id: the id of workspace
:param config: the variables of form config
:return:
"""

cls._validate_inputs_and_outputs_config(config, True)

if not config.get("cloud_provider"):
raise ValueError("cloud_provider is required")

if not config.get("api_endpoint"):
raise ValueError("api_endpoint is required")

if not config.get("api_keys"):
raise ValueError("api_keys is required")

def moderation_for_inputs(self, inputs: dict, query: str = "") -> ModerationInputsResult:
"""
Moderation for inputs.
:param inputs: user inputs
:param query: the query of chat app, there is empty if is completion app
:return: the moderation result
"""
flagged = False
preset_response = ""

if self.config['inputs_config']['enabled']:
preset_response = self.config['inputs_config']['preset_response']

if query:
inputs['query__'] = query
flagged = self._is_violated(inputs)

# return ModerationInputsResult(flagged=flagged, action=ModerationAction.OVERRIDED, inputs=inputs, query=query)
return ModerationInputsResult(flagged=flagged, action=ModerationAction.DIRECT_OUTPUT, preset_response=preset_response)

def moderation_for_outputs(self, text: str) -> ModerationOutputsResult:
"""
Moderation for outputs.
:param text: the text of LLM response
:return: the moderation result
"""
flagged = False
preset_response = ""

if self.config['outputs_config']['enabled']:
preset_response = self.config['outputs_config']['preset_response']

flagged = self._is_violated({'text': text})

# return ModerationOutputsResult(flagged=flagged, action=ModerationAction.OVERRIDED, text=text)
return ModerationOutputsResult(flagged=flagged, action=ModerationAction.DIRECT_OUTPUT, preset_response=preset_response)

def _is_violated(self, inputs: dict):
"""
The main logic of moderation.
:param inputs:
:return: the moderation result
"""
return False
65 changes: 65 additions & 0 deletions api/core/moderation/cloud_service/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"label": {
"en-US": "Cloud Service",
"zh-Hans": "云服务"
},
"form_schema": [
{
"type": "select",
"label": {
"en-US": "Cloud Provider",
"zh-Hans": "云厂商"
},
"variable": "cloud_provider",
"required": true,
"options": [
{
"label": {
"en-US": "AWS",
"zh-Hans": "亚马逊"
},
"value": "AWS"
},
{
"label": {
"en-US": "Google Cloud",
"zh-Hans": "谷歌云"
},
"value": "GoogleCloud"
},
{
"label": {
"en-US": "Azure Cloud",
"zh-Hans": "微软云"
},
"value": "Azure"
}
],
"default": "GoogleCloud",
"placeholder": ""
},
{
"type": "text-input",
"label": {
"en-US": "API Endpoint",
"zh-Hans": "API Endpoint"
},
"variable": "api_endpoint",
"required": true,
"max_length": 100,
"default": "",
"placeholder": "https://api.example.com"
},
{
"type": "paragraph",
"label": {
"en-US": "API Key",
"zh-Hans": "API Key"
},
"variable": "api_keys",
"required": true,
"default": "",
"placeholder": "Paste your API key here"
}
]
}
3 changes: 2 additions & 1 deletion api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ weaviate-client~=3.21.0
mailchimp-transactional~=1.0.50
scikit-learn==1.2.2
sentry-sdk[flask]~=1.21.1
sympy==1.12
jieba==0.42.1
celery==5.2.7
redis~=4.5.4
Expand Down Expand Up @@ -60,4 +61,4 @@ unstructured~=0.10.27
unstructured[docx,pptx,msg,md,ppt]~=0.10.27
bs4~=0.0.1
markdown~=3.5.1
google-generativeai~=0.3.2
google-generativeai~=0.3.2
2 changes: 1 addition & 1 deletion api/templates/invite_member_mail_template_en-US.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<p>Dear {{ to }},</p>
<p>{{ inviter_name }} is pleased to invite you to join our workspace on Dify, a platform specifically designed for LLM application development. On Dify, you can explore, create, and collaborate to build and operate AI applications.</p>
<p>You can now log in to Dify using the GitHub or Google account associated with this email.</p>
<p style="text-align: center;"><a style="color: #fff; text-decoration: none" class="button" href="{{ url }}">Login Here</a></p>
<p style="text-align: center;"><a class="button" href="{{ url }}">Login Here</a></p>
</div>
<div class="footer">
<p>Best regards,</p>
Expand Down
2 changes: 1 addition & 1 deletion api/templates/invite_member_mail_template_zh-CN.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<p>尊敬的 {{ to }},</p>
<p>{{ inviter_name }} 现邀请您加入我们在 Dify 的工作区,这是一个专为 LLM 应用开发而设计的平台。在 Dify 上,您可以探索、创造和合作,构建和运营 AI 应用。</p>
<p>您现在可以使用与此邮件相对应的 GitHub 或 Google 账号登录 Dify。</p>
<p style="text-align: center;"><a style="color: #fff; text-decoration: none" class="button" href="{{ url }}">在此登录</a></p>
<p style="text-align: center;"><a class="button" href="{{ url }}">在此登录</a></p>
</div>
<div class="footer">
<p>此致,</p>
Expand Down
5 changes: 4 additions & 1 deletion api/tests/integration_tests/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ LOCALAI_SERVER_URL=
COHERE_API_KEY=

# Jina Credentials
JINA_API_KEY=
JINA_API_KEY=

# Mock Switch
MOCK_SWITCH=false
Loading

0 comments on commit f1c8364

Please sign in to comment.