跳到主要内容

🥷 [S2] Challenge 11

· 阅读需 2 分钟

Just KNIME It, Season 2 / Challenge 11 reference

Challenge 11: Ask me Anything with GPT-3

Level : Hard

Description: Language models have become extremely popular, and can be efficiently incorporated into your projects through an API. You can interact with an API by sending HTTP requests to one of its endpoints, which should include input data (e.g., text or a prompt) that you want the language model to process. For this challenge, you are asked to use OpenAI's "text-davinci-003" GPT-3 model to build a KNIME workflow that answers natural language queries -- for example, "What is KNIME?". Optional: You can also built this as a KNIME component. Note: You will need the API Key from OpenAI to complete this challenge. To get it, follow the instructions in this blog post. Here's also OpenAI's documentation for making API requests. Finally, do not share your API Keys while uploading your solution to KNIME Hub.

Author: Mahantesh Pattadkal

Solution

If you are familiar with large language models, this question is relatively simple.

In fact, throughout the entire workflow using the OpenAI Python library, I only utilized one primary node: the Python view. The other nodes were secondary.

import knime.scripting.io as knio
import openai
openai.api_key = knio.flow_variables['api-key-input']

res = "Please input your question"

# create a completion
if (knio.flow_variables['chat-input'] != "") and (openai.api_key != ""):
completion = openai.Completion.create(
model="text-davinci-003",
prompt=knio.flow_variables['chat-input'],
max_tokens=200,
temperature=0
)
# the completion
res = completion.choices[0].text

part1="""<!DOCTYPE html>
<html>
<body>
"""
part2="""
</body>
</html>
"""

out = part1 + res + part2

knio.output_view = knio.view(out)

Have you found that answer incomplete? That's because the max_tokens parameter is working.

Any thoughts?

  • The gpt-3.5-turbo model is a much cheaper alternative, costing only one-tenth of the model required for the challenge. It's possible that the challenge designers were concerned about potential confusion between chat and completion, but it's unclear.
  • It's not recommended to use String Widget components for api key input. However, I encountered issues with the Credentials Widget in the Python view.
  • The Data App Header component could use some visual improvements. 😂
  • I don't know if KNIME Hub has such a feature to scan keys. If not, I think they should think about it.