Coder Social home page Coder Social logo

Comments (12)

kedashoe avatar kedashoe commented on June 15, 2024 2

ah ty @jlissner ! That explains why adding tap first fixes things as pipe uses the first function to determine the arity of the function it creates

from ramda.

kedashoe avatar kedashoe commented on June 15, 2024 1

So I think the problem is the bare JSON.parse? I'm not sure why tap seems to fix this! The simplest solution is probably just adding an arrow function

      R.pipe(
        R.tryCatch(x => JSON.parse(x), R.always({})),
        R.pathOr("", ["choices", 0, "delta"]),
        R.objOf("delta"),
        R.mergeRight({ isDone: false })
      )

We created R.bind for situations like this, but it doesn't actually seem to solve it here.. not sure exactly what is going on, maybe someone else can shed some more light

from ramda.

jlissner avatar jlissner commented on June 15, 2024 1

JSON.parse accepts 3 arguments, so ramda is auto-currying the remaining arguments, you can do R.unary(JSON.parse) to fix the issue:

R.pipe(
  R.tryCatch(R.unary(JSON.parse), R.always({})),
  R.pathOr("", ["choices", 0, "delta"]),
  R.objOf("delta"),
  R.mergeRight({ isDone: false })
)

from ramda.

orsenkucher avatar orsenkucher commented on June 15, 2024 1

Yep, I mean it's not the issue with Ramda, just was asking on how to make this github issue more indexable for other people if faced with an alike situation.

from ramda.

kedashoe avatar kedashoe commented on June 15, 2024

Hi @orsenkucher , could you post some sample input?

from ramda.

orsenkucher avatar orsenkucher commented on June 15, 2024

@kedashoe Hi!
Yes, sure!
I use this functions with deltas(data.toString()).
Here are few samples of console.log(JSON.stringify(data.toString()))

const data = /* Just paste string from sample */;
const res = deltas(data);
console.log(res);
"data: {\"id\":\"chatcmpl-7TFKQZlJPXyd33RDvEQKAV1QZKzE8\",\"object\":\"chat.completion.chunk\",\"created\":1687204790,\"model\":\"gpt-3.5-turbo-0613\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chatcmpl-7TFKQZlJPXyd33RDvEQKAV1QZKzE8\",\"object\":\"chat.completion.chunk\",\"created\":1687204790,\"model\":\"gpt-3.5-turbo-0613\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hello\"},\"finish_reason\":null}]}\n\n"
"data: {\"id\":\"chatcmpl-7TFKQZlJPXyd33RDvEQKAV1QZKzE8\",\"object\":\"chat.completion.chunk\",\"created\":1687204790,\"model\":\"gpt-3.5-turbo-0613\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"!\"},\"finish_reason\":null}]}\n\n"
"data: {\"id\":\"chatcmpl-7TFKQZlJPXyd33RDvEQKAV1QZKzE8\",\"object\":\"chat.completion.chunk\",\"created\":1687204790,\"model\":\"gpt-3.5-turbo-0613\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\".js\"},\"finish_reason\":null}]}\n\n"
"data: {\"id\":\"chatcmpl-7TFKQZlJPXyd33RDvEQKAV1QZKzE8\",\"object\":\"chat.completion.chunk\",\"created\":1687204790,\"model\":\"gpt-3.5-turbo-0613\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\" is\"},\"finish_reason\":null}]}\n\n"

from ramda.

orsenkucher avatar orsenkucher commented on June 15, 2024

Also if you call R.ifElse(..., ..., (x) => R.pipe(...)(x)) it will work.

from ramda.

orsenkucher avatar orsenkucher commented on June 15, 2024

@kedashoe Thanks for your input!

Yeah, JSON.parse seems just not to work with Ramda.
(x) => R.curry(JSON.parse)(x) breaks it again.
I'm curious too, and it'd be nice if someone else could provide further insight.

R.tap(() => {}) doesn't actually do anything, but it likely changes the context in which R.tryCatch calls JSON.parse, making the error go away. So it's better to fall to another solution.

R.bind(JSON.parse, null) isn't working as well, so using the x => JSON.parse(x) workaround is probably the best bet for now. Even though it's less elegant, at least it behaves as expected

from ramda.

orsenkucher avatar orsenkucher commented on June 15, 2024

Thanks @jlissner!
Seems obvious now ahaha. Should I rename the issue to "Auto-currying in tryCatch with JSON.parse" or something?

from ramda.

jlissner avatar jlissner commented on June 15, 2024

I don't think it would be considered an issue. Ramda has made a choice to auto curry arguments in certain settings.

You could make a post stating your case for why ramda should not auto curry in a tryCatch if you feel strongly about it.

from ramda.

kedashoe avatar kedashoe commented on June 15, 2024

I don't think it would be considered an issue. Ramda has made a choice to auto curry arguments in certain settings.

Yes ramda does this pretty much everywhere to determine arity of the functions it creates, it would be quite a change to do something different at this point. It is usually the builtins/functions which accept optional arguments which catch people out. I will close now but feel free to discuss further.

from ramda.

kedashoe avatar kedashoe commented on June 15, 2024

Yep, I mean it's not the issue with Ramda, just was asking on how to make this github issue more indexable for other people if faced with an alike situation.

Ah good thought.. probably fine to leave it, but ya something like what you suggested is good

from ramda.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.