# Script to interact with open source LLM

**URL:** <https://forum.openrefine.org/t/script-to-interact-with-open-source-llm/1551>\
**Category:** Support and Helpdesk\
**Tags:** llm\
**Created:** [May 27, 2024, 1:49pm UTC](https://forum.openrefine.org/t/script-to-interact-with-open-source-llm/1551 "2024-05-27T13:49:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![psm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/psm/32/62_2.png) [@psm](https://forum.openrefine.org/u/psm)\
**Post date:** [May 27, 2024, 1:49pm UTC](https://forum.openrefine.org/t/script-to-interact-with-open-source-llm/1551/1 "2024-05-27T13:49:04Z")

</div>

Dear all

I was trying to use open source LLMs (Llama3, Mistral etc) through Groq API. Groq supports POST for chat completion. In one column I'm having queries, and want to add another column (based on this) through chat completion API by following the example Groq has given ([GroqCloud](https://console.groq.com/docs/api-reference#chat)). This script is generating Error in OpenRefine but works fine in command prompt (python3 test3-groq.py). The error (Python/Jython) in OpenRefine is : Error: SyntaxError: mismatched input ':' expecting NEWLINE (\_types.py, line 91).

The script:

```auto
from groq import Groq
client = Groq(
    api_key='my-api-key-is-here',
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "system",
            "content": "you are a helpful assistant."
        },
        {
            "role": "user",
            "content": value,
        }
    ],
    model="llama3-70b-8192",
)

print(chat_completion.choices[0].message.content)

```

What am I missing?

Regards

Parthasarathi

---

<div class="post-metadata">

**Author:** ![thadguidry](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/thadguidry/32/653_2.png) [@thadguidry](https://forum.openrefine.org/u/thadguidry)\
**Post date:** [May 27, 2024, 2:04pm UTC](https://forum.openrefine.org/t/script-to-interact-with-open-source-llm/1551/2 "2024-05-27T14:04:07Z")

</div>

Expressions in Jython/Python must have a `return` statement.

Change that print statement to a return statement.

`return chat_completion.choices[0].message.content`

See Docs: [Jython & Clojure | OpenRefine](https://openrefine.org/docs/manual/jythonclojure)

---

<div class="post-metadata">

**Author:** ![psm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/psm/32/62_2.png) [@psm](https://forum.openrefine.org/u/psm)\
**Post date:** [May 27, 2024, 4:12pm UTC](https://forum.openrefine.org/t/script-to-interact-with-open-source-llm/1551/3 "2024-05-27T16:12:05Z")

</div>

Thanks Thad.

Applied this but the same error: Error: SyntaxError: mismatched input ':' expecting NEWLINE (\_types.py, line 91)

---

<div class="post-metadata">

**Author:** ![tfmorris](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/tfmorris/32/17_2.png) [@tfmorris](https://forum.openrefine.org/u/tfmorris)\
**Post date:** [May 27, 2024, 10:35pm UTC](https://forum.openrefine.org/t/script-to-interact-with-open-source-llm/1551/4 "2024-05-27T22:35:24Z")

</div>

> This script is generating Error in OpenRefine but works fine in command prompt (python3 test3-groq.py).

Does it work with Python 2.7? That's the version that OpenRefine bundles.

Tom

---

<div class="post-metadata">

**Author:** ![thadguidry](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/thadguidry/32/653_2.png) [@thadguidry](https://forum.openrefine.org/u/thadguidry)\
**Post date:** [May 28, 2024, 1:24am UTC](https://forum.openrefine.org/t/script-to-interact-with-open-source-llm/1551/5 "2024-05-28T01:24:43Z")

</div>

Copilot says:

> The Python script you’ve shared uses the `groq` library, which provides convenient access to the Groq REST API. However, it seems that the code snippet you posted is not directly related to Python 2.7 compatibility. Let me address that aspect for you.
> 
> **Python 2.7 Compatibility:**
> 
> - The `groq` library itself is compatible with Python 3.7 and later versions.
> - If you specifically need to work with Python 2.7, you’ll need to consider other options.
> - [For example, if you’re looking for a version of `grpcio` that supports Python 2.7, you can use version 1.39.0](https://stackoverflow.com/questions/72127334/what-is-the-last-grpcio-version-to-support-python-2-7)[1](https://stackoverflow.com/questions/72127334/what-is-the-last-grpcio-version-to-support-python-2-7). However, keep in mind that Python 2.7 has reached its official end of life, so it’s recommended to migrate to Python 3 if possible.
> 
> If you have any further questions or need assistance with a different topic, feel free to ask! 😊

---

<div class="post-metadata">

**Author:** ![psm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.openrefine.org/psm/32/62_2.png) [@psm](https://forum.openrefine.org/u/psm)\
**Post date:** [May 28, 2024, 5:19am UTC](https://forum.openrefine.org/t/script-to-interact-with-open-source-llm/1551/6 "2024-05-28T05:19:28Z")

</div>

Thanks to both of you for pointing the root cause for this issue. Now I understand why colleagues are using modules like urlbib, request etc for managing POST responses. I will try it in that way.

Regards

Parthasarathi
