- 출처 : https://python.langchain.com/docs/modules/model_io/
- 이 블로그 글은 LangChain API document의 글을 기반으로 번역되었으며 이 과정에서 약간의 내용이 추가되었습니다.
- MODEL I/O는 Prompts, Language Model, Output Parser로 이루어져 있습니다.
- 본 글에서는 Langchain에서 사용하는 LLM에 대해 간단하게 설명합니다.
Large Language Model는 LangChain의 핵심 요소입니다. LangChain은 자체적인 LLM을 제공하지는 않지만, 다른 LLM와 상호작용할 수 있는 인터페이스를 제공합니다.
LLM을 지원하는 곳은 굉장히 많습니다. 예를 들어 많은 사람들에게 알려져 있는 ChatGPT를 제공하는 OpenAI, AI개발자에게 많이 알려져 있는 Huggingface 등이 있습니다. LangChain의 LLM 클래스는 앞서 설명한 LLM과 상호작용할 수 있는 인터페이스를 제공하고 있습니다.
LLM을 활용하는 방법에 대해 다루기 전에, OpenAI의 LLM을 통해 간단한 생성을 해보겠습니다.
먼저 OpenAI의 API 키를 환경변수로 적용하여야 합니다. 이후 OpenAI의 LLM을 불러옵니다.
os.environ["OPENAI_API_KEY"] = "PERSONAL_OPENAI_API_KEY"
from langchain.llms import OpenAI
llm = OpenAI()
다음으로 간단하게 llm 객체의 call을 사용하여 문장을 생성합니다.
llm('Tell me about soccer rule')
Soccer is a game played between two teams of eleven players with a round ball on a rectangular field with goals at each end. The object of the game is to score by getting the ball into the opposing team's goal. The team that scores the most goals by the end of the game wins.
There are 17 Laws of Soccer that outline the rules of the game. These laws include rules such as the field of play, the ball, the number of players, the duration of the game, substitutions, fouls and misconduct, and the start and restart of play.
The laws also outline the responsibility of the referee, who is the ultimate authority on the field of play and has the power to enforce the laws of the game. The referee is responsible for ensuring that the rules are followed and that the game is played in a safe and fair manner.
call을 사용하는 대신, generate를 사용할 수 있습니다. generate는 문자열의 리스트를 입력으로 사용할 수 있습니다. 이를 통해 한 번에 여러 질문이 가능하고, 한번에 여러 대답을 받을 수 있습니다.
llm_result = llm.generate(["Tell me soccer rule in a 100 words", "Tell me Baseball rule in a 100 words"])
print(llm_result.generations[0][0].text)
print(llm_result.generations[1][0].text)
1. The aim of soccer is to score more goals than the opponents.
2. A soccer match is played by two teams, each with 11 players, one of whom is the goalkeeper.
3. A match consists of two 45-minute halves, with a 15-minute halftime break.
4. The ball is in play at all times and can be passed, dribbled, or shot at the goal.
5. Players may tackle an opponent to gain possession of the ball, but no physical contact is allowed.
6. A player may not touch the ball with his hands or arms, except for the goalkeeper, who may use his hands within his own penalty area.
7. A goal is scored when the ball crosses the goal line between the goalposts and underneath the crossbar.
8. If the ball goes out of play, a throw-in or corner kick is awarded to the team which did not touch it last.
9. If a team commits a foul, the opposing team is awarded a free kick.
10. If a player accumulates two yellow cards, he is sent off for the remainder of the match.
11. The team with the most goals at the end of the match is declared the winner
Baseball is a game typically played between two teams of nine players each.
The objective is to score runs by hitting the ball and running around four bases arranged in a diamond.
Each base must be reached in order and the batting team advances each base by hitting the ball in the field and then running to the next base.
The team in the field attempts to prevent runs by catching the ball and throwing it to the base before the runner reaches it.
The team with the most runs at the end of nine innings wins the game.
A regulation game consists of nine innings, in which each team has an opportunity to bat in each inning.
Other rules may include foul balls, fair balls, strikes, balls, and outs.
OpenAI의 LLM을 사용하는 경우, llm_output을 사용하여 사용한 토큰의 개수도 확인할 수 있습니다.
llm_result.llm_output
{'token_usage': {'total_tokens': 417,
'completion_tokens': 401,
'prompt_tokens': 16},
'model_name': 'text-davinci-003'}
[MODEL I/O - Langauge Models] LLM How to - 2 (1) | 2023.07.12 |
---|---|
[MODEL I/O - Langauge Models] LLM How to - 1 (0) | 2023.07.05 |
[MODEL I/O - Prompts] Example selectors (0) | 2023.07.04 |
[MODEL I/O - Prompts] Prompt templates - 3 (0) | 2023.07.03 |
[MODEL I/O - Prompts] Prompt templates - 2 (0) | 2023.06.30 |
댓글 영역