huggingface / chat-ui

Open source codebase powering the HuggingChat app
https://huggingface.co/chat
Apache License 2.0
6.88k stars 976 forks source link

Expected double-quoted property name in JSON #670

Closed flexchar closed 6 months ago

flexchar commented 7 months ago

Hi! I've tried this end of October and now giving another shot. Both times I end up facing issues with getting the config right as I want because of various JSON parsing issues...

This is what I'm stuck with at the time of writing this issue and same I had in the past

2:45:31 PM [vite] .env.local changed, restarting server...

2:45:31 PM [vite] server restarted.
2:45:33 PM [vite] Error when evaluating SSR module /src/lib/server/models.ts:
|- SyntaxError: Expected double-quoted property name in JSON at position 191 (line 10 column 5)
    at JSON.parse (<anonymous>)
    at /Users/luke/pill/chat-ui/src/lib/server/models.ts:71:51
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async instantiateModule (file:///Users/luke/pill/chat-ui/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)

2:45:33 PM [vite] Error when evaluating SSR module /src/routes/+layout.server.ts: failed to import "/src/lib/server/models.ts"
|- SyntaxError: Expected double-quoted property name in JSON at position 191 (line 10 column 5)
    at JSON.parse (<anonymous>)
    at /Users/luke/pill/chat-ui/src/lib/server/models.ts:71:51
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async instantiateModule (file:///Users/luke/pill/chat-ui/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)

SyntaxError: Expected double-quoted property name in JSON at position 191 (line 10 column 5)
    at JSON.parse (<anonymous>)
    at /Users/luke/pill/chat-ui/src/lib/server/models.ts:71:51
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async instantiateModule (file:///Users/luke/pill/chat-ui/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)
SyntaxError: Expected double-quoted property name in JSON at position 191 (line 10 column 5)
    at JSON.parse (<anonymous>)
    at /Users/luke/pill/chat-ui/src/lib/server/models.ts:71:51
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async instantiateModule (file:///Users/luke/pill/chat-ui/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:54405:9)

It appears that something goes wrong and it's impossible to see where is the typo or if there is even one. While this is my config. I understand that I might be missing an obvious syntax thing for JSON but squeezing json in env makes it extremely hard.

# In .env.local

MONGODB_URL=mongodb://localhost:27017
MONGODB_DB_NAME=chat-ui
MONGODB_DIRECT_CONNECTION=false

# https://github.com/huggingface/chat-ui/pull/541/files#diff-11277a91257f93f2e86ab0ce31a7e5dc8f604537cb4f73c9db05731735edc7ee
MODELS=`[
  {
    "name": "custom",
    "websiteUrl": false,
    "parameters": {
      "temperature": 0.7,
      "repetition_penalty": 1.2,
      "truncate": 1000,
      "max_new_tokens": 1024,
    },
    "endpoints": [{
      "type" : "openai",
      "baseURL": "http://localhost:8787/api/custom/v1"
    }]
  },
  {
    "name": "mixtral",
    "parameters": {
      "temperature": 0,
    },
    "endpoints": [{
      "type" : "openai",
      "model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
      "baseURL": "https://api.deepinfra.com/v1/openai",
      "apiKey": "..."
    }]
  }
]`

TASK_MODEL="mixtral"

# PUBLIC_ORIGIN=#https://huggingface.co
# PUBLIC_SHARE_PREFIX=#https://hf.co/chat
# PUBLIC_GOOGLE_ANALYTICS_ID=#G-XXXXXXXX / Leave empty to disable
# PUBLIC_ANNOUNCEMENT_BANNERS=`[
#     {
#     "title": "Llama v2 is live on HuggingChat! 🦙",
#     "linkTitle": "Announcement",
#     "linkHref": "https://huggingface.co/blog/llama2"
#   }
# ]`

# requests per minute
RATE_LIMIT=6
# how many messages a user can send in a conversation before having to login. set to 0 to force login right away
MESSAGES_BEFORE_LOGIN=20

As this issue is opened, I would like to propose handling JSON data in a dedicated config file such as models.json, or even better, models.ts that would be imported. This would allow to tap into existing editing tools to verify the syntax. The very least I'd like to suggest to print an exact location for faulty json to assist in debugging. The current error message is arguably useless.

I did search for such issue but Github returned no results, I am quite surprised that as popular as the project is, no one else has bumped into.

Related:

flexchar commented 7 months ago

Okay, I had trailing commas. They're automatically removed by my IDE on JSON files so I am blind to them. Perhaps something like json5 could be used instead of JSON.parse?

https://json5.org/

PS. I've also confidently observed that correcting incorrect file will not relieve error unless CLI npm run dev is stopped and started manually.

nsarrazin commented 6 months ago

I think it'd be nice to use json5 if we can, not sure if it would have side effects, I'll try it out.

nsarrazin commented 6 months ago

Not the first time we faced this issue, so I took the time to open this PR let me know if it solves your issues.

flexchar commented 6 months ago

It definitely does, and I agree on a dedicated config file. I tried using .env with docker compose and it rejected the approach. I've been thinking one could also inject MODELS variable using Vite config from a dedicated models.ts file. That would provide type hinting and flexibility to trailing commas without any addition dependencies. What do you think about that? :)

nsarrazin commented 6 months ago

Merged the other pr! I think a rework of the settings is in order, so we'll definitely think about all this, I´d be curious to know what the community prefers for organizing config settings.