Problems with UTF-8

Started by JohnClaw, April 28, 2024, 09:37:07 PM

Previous topic - Next topic

JohnClaw

GUI "AIChat", PIXELS

  GLOBAL Form1 AS CONTROL
  GLOBAL Edit1 AS CONTROL
  GLOBAL Edit2 AS CONTROL
  GLOBAL Btn1  AS CONTROL
   
SUB FORMLOAD
       
   Form1 = BCX_FORM("BCX_Dllama_GUI", 0, 0, 1900, 700)
   Edit2 = BCX_EDIT("Here you should type your question for Dllama.", Form1, 3, 10, 20, 1850, 40)
   Edit1 = BCX_EDIT("Here Dllama types it's answer for your question.", Form1, 2, 10, 100, 1850, 300)
   Btn1  = BCX_BUTTON("Start", Form1, 1, 400, 500, 135, 45)
   BCX_SET_FONT(Btn1, "Tahoma", 14)
   BCX_SET_FONT(Edit1, "Tahoma", 14)
   BCX_SET_FONT(Edit2, "Tahoma", 14,)
   
   CENTER(Form1)
   
   SHOW(Form1)
END SUB
   
BEGIN EVENTS
   SELECT CASE CBMSG
   CASE WM_COMMAND
    LOCAL question$
    LOCAL LModelName$
    LOCAL answer$

     IF CBCTL = 1 THEN
     question$ = BCX_GET_TEXT$(Edit2)
     DECLARE FUNCTION LLM$ LIB "Dllama.dll" ALIAS "Dllama_Simple_Inference" (A AS STRING, B AS STRING, C AS STRING, D AS BOOLEAN, E AS UINT, X AS UCHAR, F AS STRING )
     LModelName$ = "phi3:4B:Q4"
     answer$ = WIDETOANSI$(LLM("C:\LLM\gguf\", "models.json", LModelName$, FALSE, 1024, 27, ANSITOWIDE$(question$, CP_UTF8)), CP_UTF8)
      BCX_SET_TEXT(Edit1, answer$)
      END IF   
     
   END SELECT

END EVENTS

When i tried to compile it, got these errors:

C:\BED\Demos\Gui_Demo\Calc1\Calc.c(621): error #2140: Type error in argument 7 to 'function'; expected 'char *' but found 'wchar_t *'.
C:\BED\Demos\Gui_Demo\Calc1\Calc.c(621): error #2140: Type error in argument 1 to 'WideToAnsi'; expected 'wchar_t *' but found 'char *'.
POLINK: fatal error: File not found: 'C:\BED\Demos\Gui_Demo\Calc1\Calc.obj'.

What i did wrong? How can i fix it? Could you help me, please?

The dll function is expecting utf8 string pointer for unicode support and it returns utf8 data out.

airr

DECLARE FUNCTION LLM$

Adding the '$' sigil causes the function to return a normal string, not a wide string

ANSITOWIDE$(question$, CP_UTF8)

Your 7th argument is expecting a normal string, as configured in the "DECLARE FUNCTION" line.  Maybe change that to expect a wide string instead?

Just guessing here....

AIR.

JohnClaw

Thanks for reply. But how can i DIM a wide string in BCX? I'm a newbie and don't know how to do that.

MrBcx

This now compiles but I don't know if it is correct.  You will need to test it.



GUI "AIChat", PIXELS

GLOBAL Form1 AS CONTROL
GLOBAL Edit1 AS CONTROL
GLOBAL Edit2 AS CONTROL
GLOBAL Btn1  AS CONTROL

SUB FORMLOAD
    Form1 = BCX_FORM("BCX_Dllama_GUI", 0, 0, 1900, 700)
    Edit2 = BCX_EDIT("Here you should type your question for Dllama.", Form1, 3, 10, 20, 1850, 40)
    Edit1 = BCX_EDIT("Here Dllama types it's answer for your question.", Form1, 2, 10, 100, 1850, 300)
    Btn1  = BCX_BUTTON("Start", Form1, 1, 400, 500, 135, 45)
    BCX_SET_FONT(Btn1, "Tahoma", 14)
    BCX_SET_FONT(Edit1, "Tahoma", 14)
    BCX_SET_FONT(Edit2, "Tahoma", 14, )
    CENTER(Form1)
    SHOW(Form1)
END SUB

BEGIN EVENTS
    SELECT CASE CBMSG
    CASE WM_COMMAND
        LOCAL question$
        LOCAL LModelName$
        LOCAL answer$

        IF CBCTL = 1 THEN
            question$ = BCX_GET_TEXT$(Edit2)
            DECLARE FUNCTION LLM$ LIB "Dllama.dll" ALIAS "Dllama_Simple_Inference" (A AS STRING, B AS STRING, C AS STRING, D AS BOOLEAN, E AS UINT, X AS UCHAR, F AS WSTRING ) AS WSTRING
            LModelName$ = "phi3:4B:Q4"
            answer$ = WIDETOANSI$(LLM("C:\LLM\gguf\", "models.json", LModelName$, FALSE, 1024, 27, ANSITOWIDE(question$, CP_UTF8)), CP_UTF8)
            BCX_SET_TEXT(Edit1, answer$)
        END IF
    END SELECT
END EVENTS


MrBcx

I just noticed you uploaded Dllama.dll, so I did some experimenting myself.

If I treat all strings as ansi strings, not wstrings, it seems to work but I don't have any LLM's on my PC.

See my attached screenshot.

Here is the new code.


GUI "AIChat", PIXELS

GLOBAL Form1 AS CONTROL
GLOBAL Edit1 AS CONTROL
GLOBAL Edit2 AS CONTROL
GLOBAL Btn1  AS CONTROL

SUB FORMLOAD
    Form1 = BCX_FORM("BCX_Dllama_GUI", 0, 0, 1900, 700)
    Edit2 = BCX_EDIT("Here you should type your question for Dllama.", Form1, 3, 10, 20, 1850, 40)
    Edit1 = BCX_EDIT("Here Dllama types it's answer for your question.", Form1, 2, 10, 100, 1850, 300)
    Btn1  = BCX_BUTTON("Start", Form1, 1, 400, 500, 135, 45)
    BCX_SET_FONT(Btn1, "Tahoma", 14)
    BCX_SET_FONT(Edit1, "Tahoma", 14)
    BCX_SET_FONT(Edit2, "Tahoma", 14, )
    CENTER(Form1)
    SHOW(Form1)
END SUB

BEGIN EVENTS
    SELECT CASE CBMSG
    CASE WM_COMMAND
        LOCAL question$
        LOCAL LModelName$
        LOCAL answer$

        IF CBCTL = 1 THEN
            question$ = BCX_GET_TEXT$(Edit2)
            DECLARE FUNCTION LLM$ LIB "Dllama.dll" ALIAS "Dllama_Simple_Inference" (A AS STRING, B AS STRING, C AS STRING, D AS BOOLEAN, E AS UINT, X AS UCHAR, F AS STRING ) AS STRING
            LModelName$ = "phi3:4B:Q4"
            answer$ = LLM("C:\LLM\gguf\", "models.json", LModelName$, FALSE, 1024, 27, question$)
            BCX_SET_TEXT(Edit1, answer$)
        END IF
    END SELECT
END EVENTS


MrBcx

John,

The .dll clearly is setup for ansi strings, not wide strings.  ( See my previous screenshot )

To use wide strings, you will need to get your hands on a version of the .dll that supports them.

Do you have "models.json" and at least one LLM on your PC to test?

I am downloading codellama_7b.gguf right now but I don't know where to find a copy of "models.json" that I can edit.

JohnClaw

Thank you very much for helping me, Mr. Bcx. I tested the edited code. I wrote a question in Russian but the app answered in Chinese. So the test was failed. Something gone wrong. I wrote: "Почему коты боятся собак?" "Why cats are afraid of dogs?" and got this: 瑉愠灰慥獲琠慨⁴桴⁥湩異⁴潹❵敶瀠潲楶敤⁤潣瑮楡獮渠湯猭慴摮牡⁤档牡捡整獲潈敷敶Ⱳ椠⁦潹❵敲氠潯楫杮琠楤捳獵⁳⁡潴楰⁣牯渠敥⁤湩潦浲瑡潩Ɱ映敥牦敥琠牰癯摩⁥⁡汣慥⁲湡⁤潣敨敲瑮


JohnClaw

Yes, i have models.json. See attachment.

JohnClaw


MrBcx

#9
Quote from: JohnClaw on April 28, 2024, 11:59:15 PM
You should use Phi 3 model because models.json has chat template only for this model: https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-gguf/resolve/main/Phi-3-mini-4k-instruct-q4.gguf?download=true

Thank you ... Having all the required files allows for better testing / experimenting.

I will provide a screenshot if / when I get everything working.

JohnClaw

This file also may help. (in attachment)

JohnClaw

And the most important one: Dllama.h (attached to post).

MrBcx

Quote from: JohnClaw on April 29, 2024, 12:24:24 AM
This file also may help. (in attachment)

Please share the URL where you obtained the dll and lib. 
I might find additional info there that can be used to help resolve your issues.


MrBcx

See attached screenshot ... it is working great on my computer.